(この記事は Go by Example: Values を翻訳したものです。)
Goにはstring, integer, float, booleanなどのさまざまな方があるが、ここでは例を少し紹介する。
package main
import "fmt"
func main() {
// Stringsは+で足すことができる。
fmt.Println("go" + "lang")
// intgers と float
fmt.Println("1+1 =", 1+1)
fmt.Println("7.0/3.0 =", 7.0/3.0)
// Booleansとあなたがいつも使っているようなオペレータ
fmt.Println(true && false)
fmt.Println(true || false)
fmt.Println(!true)
}
$ go run values.go
golang
1+1 = 2
7.0/3.0 = 2.3333333333333335
false
true
false
int - float
package main
import "fmt"
func main() {
// int / int
fmt.Println("1/3 =", 1/3)
// int / float
fmt.Println("1/3.0 =", 1/3.0)
}
$ go run int-float.go
1/3 = 0
1/3.0 = 0.3333333333333333
pythonの2系と同じ結果
int / intはfloatにならない。