LoginSignup
40
13

More than 5 years have passed since last update.

Go言語のHello Worldを3分で試す

Last updated at Posted at 2016-04-22

golang-project.png

Go言語でHello Worldしてみたくなったので挑戦してみる。

環境

  • MacBook Air (11-inch, Early 2015)
  • Mac OS X El Capitan バージョン 10.11.4

インストール

brewに期待。

$ brew install go
==> Downloading https://homebrew.bintray.com/bottles/go-1.6.el_capitan.bottle.2.
######################################################################## 100.0%
==> Pouring go-1.6.el_capitan.bottle.2.tar.gz
==> Caveats
As of go 1.2, a valid GOPATH is required to use the `go get` command:
  https://golang.org/doc/code.html#GOPATH

You may wish to add the GOROOT-based install location to your PATH:
  export PATH=$PATH:/usr/local/opt/go/libexec/bin
==> Summary
🍺  /usr/local/Cellar/go/1.6: 5,771 files, 324.9M

インストール完了。ほんとにこれでいいのかな?というレベル。

$ which go
/usr/local/bin/go

コマンドも実行できそうだ。

Hello Worldの準備

とりあえず、hello.goを書いてみる。

hello.go
package main

import "fmt"

func main() {
  fmt.Printf("Hello World\n")
}

実行

実行してみる。

$ go run hello.go
Hello World

もうできた!

バイナリを作って実行もできるらしい。

$ go build hello.go
$ ls
hello*      hello.go

おお、バイナリができておる。早速、できたてのバイナリを実行してみる。

$ ./hello
Hello World

あっという間にできた!

参考

40
13
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
40
13