Golang的类型定义

数字类型

func main(){
    fmt.Println("1 + 1 =", 1+1)//2
    fmt.Println("10 - 1 =", 10-1)//9
    fmt.Println("10 / 3 =", 10/3)//3
    fmt.Println("10.0 / 3 =", 10.0/3)//3.3333333333333335
    fmt.Println("10 % 3 =", 1+1)//2
}

字串类型

func main(){
    fmt.Println("Hello World")//Hello World
    fmt.Println("Hello" + "World")//Hello World
    fmt.Println(string("Hello World"[3]))//l

    var s string = "Hello World"
    s = strings.Replace(s, "H", "X", 2)//文字列sにある1番目と2番目のHをXに変換
    fmt.Println(s)//Xello World
    fmt.Println(strings.Contains(s, "World"))//true sにWorldが含まれているか判定
}

类型转换

func main(){
    var x int = 1
    xx := float64(x)
    fmt.Printf("%T %v\n", xx, xx)

    var s string = "14"
    i, _ :=strconv.Atoi(s)//返り値が2つあるためiと_を宣言。
    fmt.Printf("%T %v", i, i)
}

数组

func main(){
    var a[2]int//配列内は2つだけ
    a[0] = 100
    a[1] = 200
    fmt.Println(a)//[100 200]

    var b[2]int = [2]int{100, 200}
    fmt.Println(b)//[100 200]

    var c[]int = []int{100, 200}
    c[0] = 500
    c[1] = 600
    c = append(c, 700)//配列に値追加
    fmt.Println(c)//[500 600 700]



    n := []int{1, 2, 3, 4, 5}
    fmt.Println(n)//[1 2 3 4 5]
    fmt.Println(n[2])//3
    fmt.Println(n[2:4])//[3 4]
    fmt.Println(n[:4])//[1 2 3 4]
    fmt.Println(n[2:])//[3 4 5]
    fmt.Println(n[:])//[1 2 3 4 5]

    n[2] = 100
    fmt.Println(n)//[1 2 100 4 5]

    var board = [][]int{
        []int{0, 1, 2},
        []int{3, 4, 5},
        []int{6, 7, 8},
    }

    fmt.Println(board)//[[0 1 2] [3 4 5] [6 7 8]]

    n = append(n, 100, 200, 300)
    fmt.Println(n)//[1 2 100 4 5 100 200 300]

}

切成薄片 (qiē


func main(){
    n := make([]int, 3, 5)//(型, 長さ, キャパシティ)
    fmt.Printf("len=%d cap=%d value=%v\n", len(n), cap(n), n)//len=3 cap=5 value=[0 0 0]

    var c []int
    c = make([]int, 5)
    for i :=0; i < 5; i++{
        c = append(c, i)
    }
    fmt.Println(c)//[0 0 0 0 0 0 1 2 3 4]

    c = make([]int, 0, 5)
    for i :=0; i < 5; i++{
        c = append(c, i)
    }
    fmt.Println(c)//[0 1 2 3 4]
}

地圖(連結數組)

func main(){
    m := map[string]int{"apple": 100, "banana": 200}
    fmt.Println(m)//map[apple:100 banana:200]
    fmt.Println(m["apple"])//100
    m["banana"] = 300
    m["new"] = 500
    fmt.Println(m)//map[apple:100 banana:300 new:500]

    v, ok := m["apple"]
    fmt.Println(v, ok)//100 true

    v2, ok2 := m["nothing"]
    fmt.Println(v2, ok2)//0 false

    m2 := make(map[string]int)
    m2["pc"] = 5000
    fmt.Println(m2)//map[pc:5000]

    var a map[string]int = map[string]int{"apple": 100, "banana": 200}
    fmt.Println(a)//map[apple:100 banana:200]

}

兼职

func main(){
    b := []byte{72, 73}
    fmt.Println(b)//[72 73]
    fmt.Println(string(b))//HI

    c := []int{72, 73}
    fmt.Println(c)//[72 73]
    //fmt.Println(string(c))//変換できない
}

函数

func add(x, y int) (int, int) {
    fmt.Println("add function")
    return x + y, x - y
}

func cal(price, item int) (result int){
    result = price * item
    return
}

func convert(price int) float64{
    return float64(price)
}

func foo(params ...int){
    fmt.Println(len(params), params)
}

func main(){
    r1, r2 := add(10, 20)
    fmt.Println(r1, r2)

    r3 := cal(100, 2)
    fmt.Println(r3)

    f := func(x int){
        fmt.Println("inner func")
    }
    f(1)

    foo(10, 20)//[10 20]
    foo(10, 20, 30)//[10 20 30]

    s := []int{1, 2, 3}
    foo(s...)//[1 2 3]
}

闭包

func incrementGenerater() (func() int) {
    x := 0
    return func() int {
        x++
        return x
    }
}

func circleArea(pi float64) func(radius float64) float64{
    return func(radius float64) float64{
        return pi * radius * radius
    }
}

func main(){
    counter := incrementGenerater()
    fmt.Println(counter())//1
    fmt.Println(counter())//2
    fmt.Println(counter())//3
    fmt.Println(counter())//4

    c1 := circleArea(3.14)
    fmt.Println(c1(2))//12.56

    c2 := circleArea(3)
    fmt.Println(c2(2))//12
}


广告
将在 10 秒后关闭
bannerAds