べすとえふぉーと

プログラミング等のノート 

go

Go 使い方メモ ゴルーチン チャネル

go

package main import "fmt" func main() { //make channel c := make(chan int) //send function go func(send chan<- int) { for i := 0; i < 8; i++ { send <- i } close(send) }(c) //receive roop for { value,ok := <-c if !ok { break } //receive fmt…

Go 使い方メモ ゴルーチン

go

元がゴルーチンのtest()実行後3秒で終わるので、test()は2まで表示して終了する package main import( "fmt" "time" ) func main() { fmt.Println("Start") fmt.Println("normal test") test() fmt.Println("go routine test") go test() time.Sleep(3 * tim…

Go 使い方メモ スライス マップ

go

package main import ( "fmt" ) func doubl(valueary []int) { for i:=0; i< len(valueary); i++ { valueary[i] *= 2 } } func main() { strary := [5]string{"a","b","c","d","e"} var slc1 []string slc1 = strary[:] fmt.Println(slc1) slc2 := strary[1:…

Go 使い方メモ インタフェース

go

package main import "fmt" type Calc interface { Calc(x int, y int) int } type Add struct { } func (r Add) Calc(x int, y int) int{ return x + y } type Sub struct { } func (r Sub) Calc(x int, y int) int{ return x - y } func main() { var add …

Go 使い方メモ 2

go

package main import ( "fmt" ) type myType int func for_range(array []int) { for i := range array { if i % 2 == 0 && array[i] % 2 == 0 { fmt.Println(array[i]) } } } func (value myType) printmethod() { fmt.Println(value) } func (value *myTyp…

Go 使い方メモ 1

go

package main import ( "strconv" "fmt" ) type struct_test struct { X int Y int } func add(x int,y int) int { return x + y } func add_minus(x, y int)(string,string) { addval := x + y minusval := x - y return strconv.Itoa(addval),strconv.Itoa…

Macでmgoをインストールしようとしたら "bzr": executable file not found

go

Mongoのgoドライバ,mgoを入れようとしたらBazaarがないよと go: missing Bazaar command. See http://golang.org/s/gogetcmd package labix.org/v2/mgo: exec: "bzr": executable file not found in $PATHBazaarが足りないのでbrewで入れる brew install bzr…

Macでgo get

go

Macでgo getしようとすると cannot download, $GOPATH must not be set to $GOROOTが出るので対応方法 パスを確認 which go/usr/local下にあると permission denyされちゃうので sudo suGOPATHを設定 export GOPATH=/usr/local/go/bin設定したら go get gith…