5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Goでつまづいたところ

Last updated at Posted at 2016-09-15

Go以前の話

El capitanでbrewを使う
sudo chown -R (whoami) /usr/local && brew prune && brew update

Macにインストール

古いgoは直接ディレクトリを消す。
sudo rm -r /usr/local/bin/go
古いpathを見ている場合は brew unlink go する
brew install go

GOPATH

brew install goでMACにインストールした場合、GoPATHは次のように書いた

 export PATH=$PATH:$GOROOT/bin
 export GOROOT=/usr/local/Cellar/go/1.7.1/libexec
 export GOPATH=$HOME/_go <=好きなところ
 

httpサーバとstats確認

go-lang-api-stats-handerを使用

go get github.com/fukata/golang-stats-api-handler

sampleを以下変更

gostats.go

package main
import (
    "fmt"
    "net/http"
    "log"
    "github.com/fukata/golang-stats-api-handler"
)

func handler(w http.ResponseWriter, r *http.Request) {
  fmt.Fprintf(w, "Hello, World")
}

func main() {
    http.HandleFunc("/", stats_api.Handler)
    log.Fatal( http.ListenAndServe(":8080", nil) )
}

結果

$ curl -i http://localhost:8080
HTTP/1.1 200 OK
Content-Length: 513
Content-Type: application/json
Date: Thu, 15 Sep 2016 11:48:01 GMT

{"time":1473940081157567012,"go_version":"go1.7.1","go_os":"darwin","go_arch":"amd64","cpu_num":2,"goroutine_num":3,"gomaxprocs":2,"cgo_call_num":1,"memory_alloc":313712,"memory_total_alloc":313712,"memory_sys":3180792,"memory_lookups":7,"memory_mallocs":4896,"memory_frees":144,"memory_stack":294912,"heap_alloc":313712,"heap_sys":753664,"heap_idle":114688,"heap_inuse":638976,"heap_released":0,"heap_objects":4752,"gc_next":4194304,"gc_last":0,"gc_num":0,"gc_per_second":0,"gc_pause_per_second":0,"gc_pause":[]}

specの悪いPC:cry:

学んだこと

package mainは書かないとエラーとなり実行できない。
test.go:1:1: expected 'package', found 'import'

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?