Goal
Log
インストールしてみる
[13:05:31] % brew search go algol68g go-app-engine-32 gocr google-sparsehash jpegoptim pygobject bogofilter go-app-engine-64 goffice google-sql-tool mongodb ringojs fuego go-gui google-app-engine gosu mongoose strategoxt ggobi goaccess google-js-test gource pango go gobject-introspection google-perftools gst-plugins-good pangomm josegonzalez/php/php53-mongo josegonzalez/php/php54-mongo josegonzalez/php/php55-mongo homebrew/versions/gst-plugins-good010 homebrew/games/fuego homebrew/games/gnu-go homebrew/games/go-gui [13:06:28] % brew install go ==> Downloading http://go.googlecode.com/files/go.go1.src.tar.gz ######################################################################## 100.0% ==> ./make.bash /usr/local/Cellar/go/1: 3559 files, 105M, built in 61 seconds [13:07:34] % which go /usr/local/bin/go [13:08:57] % go --version flag provided but not defined: -version Go is a tool for managing Go source code. Usage: go command [arguments] The commands are: build compile packages and dependencies clean remove object files doc run godoc on package sources env print Go environment information fix run go tool fix on packages fmt run gofmt on package sources get download and install packages and dependencies install compile and install packages and dependencies list list packages run compile and run Go program test test packages tool run specified go tool version print Go version vet run go tool vet on packages Use "go help [command]" for more information about a command. Additional help topics: gopath GOPATH environment variable packages description of package lists remote remote import path syntax testflag description of testing flags testfunc description of testing functions Use "go help [topic]" for more information about that topic. [13:09:06] % go version go version go1 [13:09:11] %
ふむふむ。 go run と打てばいいのかな
ファイル作ってコンパイル&実行してみる
とりあえず適当なファイル作って叱られてみよう
[13:09:11] % cd ~/prj/go [13:10:28] % mkdir myFirstGo [13:10:35] % cd myFirstGo [13:10:54] % echo "hogeeee" > first.go [13:11:06] % cat first.go hogeeee [13:11:09] % go run first.go package : first.go:1:1: expected 'package', found 'IDENT' hogeeee [13:11:19] %
ハローりっちゃん
[13:15:49] % vi first.go [13:15:49] % cat first.go package main import "fmt" func main() { fmt.Printf("ハロー、りっちゃん!\n") } [13:15:53] % go run first.go ハロー、りっちゃん! [13:15:57] %
あらやだ、簡単...
FizzBuzzつくる
せっかくなので最近入れたIntellij/IDEA使ってみようっと♪
create new projectで、
既存のmyFirstGoディレクトリにマッピング
で、fizzbuzz.goファイル作ろうとしたら...
oh... GOプラグインとか入れないといけないっぽいな...
とりあえず(txtと認識されつつ...orz) IntelliJ/IDEAで編集してみよう
[13:34:24] % cat fizzbuzz.go package main import "fmt" func main() { fmt.Print("Let's start FizzBuzz!\n") for i := 0; i < 30; i++ { s := "--" if i % (3*5) == 0 { s = "FizzBuzz!!" } else if i % 5 == 0 { s = "Buzz" } else if i % 3 == 0 { s = "Fizz" } else { // Do Nothing } fmt.Printf("%d\t: %s\n", i, s) } } [13:34:43] %
実行までIntelliJの中でできるはずだけど
- まだ慣れてない
- そもそもGOプラグインが無いから実行できないかもしらん
ということでcliから
[13:37:28] % go run fizzbuzz.go Let\'s start FizzBuzz! 0 : FizzBuzz!! 1 : -- 2 : -- 3 : Fizz 4 : -- 5 : Buzz 6 : Fizz 7 : -- 8 : -- 9 : Fizz 10 : Buzz 11 : -- 12 : Fizz 13 : -- 14 : -- 15 : FizzBuzz!! 16 : -- 17 : -- 18 : Fizz 19 : -- 20 : Buzz 21 : Fizz 22 : -- 23 : -- 24 : Fizz 25 : Buzz 26 : -- 27 : Fizz 28 : -- 29 : -- [13:37:30] %
とりあえずできた
ローカルパッケージを作ってimportしてみる
[13:14:47] % cd ~/prj/go/myFirstGo [13:14:53] % tree . ├── main.go └── mylib └── fizzbuzz.go 1 directory, 2 files [13:14:55] %
main.go
package main import ( "./mylib" ) func main() { fizzbuzz.Run() }
./mylib/fizzbuzz.go
package fizzbuzz import "fmt" func Run() { fmt.Print("Let's start FizzBuzz!\n") for i := 0; i < 30; i++ { s := "--" if i % (3*5) == 0 { s = "FizzBuzz!!" } else if i % 5 == 0 { s = "Buzz" } else if i % 3 == 0 { s = "Fizz" } else { // Do Nothing } fmt.Printf("%d\t: %s\n", i, s) } }
で、いけた
参考: http://system.hateblo.jp/entry/2013/06/22/020918