LoginSignup
2

More than 5 years have passed since last update.

Go by Example: Values

Last updated at Posted at 2015-01-24

(この記事は 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にならない。

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
2