Go言語とはなにか?

$ pwd
/home/vagrant/golang_lessons
$ sudo yum -y install golang
$ go version
go version go1.6.3 linux/amd64

はじめてのGoプログラム

package main

import "fmt"

func main() {
    fmt.Println("hello world")
}
$ go build hello.go
$ ls
hello  hello.go
$ ./hello
hello world
$ go run hello.go
hello world

変数を使ってみよう

func main() {
    var msg string
    msg = "hello world"
    fmt.Println(msg)
}
func main() {
    var msg = "hello world"
    fmt.Println(msg)
}
func main() {
    msg := "hello world"
    fmt.Println(msg)
}
$ go run hello.go
hello world
func main() {
    // var a, b int
    // a, b = 10, 15
    a, b := 10, 15
}
func main() {
  var (
      c int
      d string
  )
  c = 20
  d = "hoge"
}

基本データ型を使ってみよう

package main

import "fmt"

func main() {
    a := 10
    b := 12.3
    c := "hoge"
    var d bool
    fmt.Printf("a:%d, b:%f, c:%s, d:%t\n", a, b, c, d)
}
$ go run hello.go
a:10, b:12.300000, c:hoge, d:false

定数を使ってみよう

package main

import "fmt"

func main() {
    const name = "taguchi"
    name = "fkoji"
    fmt.Println(name)
}
$ go run hello.go
# command-line-arguments
./hello.go:7: cannot assign to name
package main

import "fmt"

func main() {
    const (
        sun = iota // 0
        mon // 1
        tue // 2
    )
    fmt.Println(sun, mon, tue)

}
$ go run hello.go
0 1 2

トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS