:>/dev/null

ラガードエンジニアの不撓不屈の精神

goでHello World

■install

  # vim go.sh
  export PATH=$PATH:/usr/local/go/bin
  • /usr/local/go/bin/go version

■config

●niginx

 # vim /etc/nginx/nginx.conf

    location / {
        try_files $uri $uri.html $uri/index.html @rails-unicorn;
    }

    location /golang {
        fastcgi_pass 127.0.0.1:8190;
        include fastcgi.conf;
    }

■code

  • sample.go
package main

import (
  "fmt"
  "net"
  "net/http"
  "net/http/fcgi"
)

func viewHandler(res http.ResponseWriter, req *http.Request) {
  fmt.Fprintf(res, "<h1>%s</h1><div>%s</div>", "'Hello World", "fastCGI ")
}

func main() {
  l, err := net.Listen("tcp", "127.0.0.1:8190")
    if err != nil {
        return
    }
    http.HandleFunc("/", viewHandler)
    fcgi.Serve(l, nil)
}

■go run

  • systemctl start nginx.service
  • systemctl status nginx.service
  • cd {インストールディレクトリ}/
  • go run sample.go

■go build

  • go build sample.go
  • /samlpe &