まずは最初はHello World
さて、やっぱり最初はHello Worldを表示したいという事でHello Worldから行ってみましょう。
シンプルにHello World
//hello_world.d
import std.stdio;
void main(){
writef("Content-Type: text/plain;encoding=utf-8\n\nHello World!");
}
是だけです。とりあえずコンパイルして実行してみましょう。
Content-Type: text/plain;encoding=utf-8 Hello World!
後はパーミッションを修正してApacheさんに起動してもらえばApacheさんがリクエストを整えてくれます。
変数を使ってHello World
//hello_world_2.d
import std.stdio;
void main(){
const char[] str = "Hello World!";
writef("Content-Type: text/plain;encoding=utf-8\n\n%s",str);
}
ほとんどさっきと同じですが文字列を変数にしています。
Content-Type: text/plain;encoding=utf-8 Hello World!
注意して欲しいのはwritefの第一引数は%を特殊に扱ってるため、第一引数に%を含む可能性のある変数を渡すとバグの原因になります。自由に入れられる変数を書き出す場合には必ずwritef("%s",変数);という風にしましょう。