golang 学习总结1

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yefengzhichen/article/details/82355085

1.golang字符串range时返回的类型为rune

在某次测试时发现,string字符串,直接用下标访问和用range访问返回的类型不同,参看下面:

func main () {
	str := "aA"
	fmt.Printf("type is %T \n", str[1], )
	for _, i := range str {
	    fmt.Printf("is type %T\n", i)
	}
}

输出结果为:

type is uint8 
is type int32
is type int32

即直接下标访问和用range访问返回的结果不同。

2.结构体初始化

定义一个结构体:

type Rect struct {

    x, y float64

    width, height float64

}

结构体指针的初始化方法:

rect1 := new(Rect)

rect2 := &Rect{}

rect3 := &Rect{0, 0, 100, 200}

rect4 := &Rect{width:100, height:200}

结构体类型的初始化方法:

rect1 := Rect{}

rect2 := Rect{0, 0, 100, 200}

rect3 := Rect{width:100, height:200}

则表示这个是一个Rect{}类型.两者是不一样的.

猜你喜欢

转载自blog.csdn.net/yefengzhichen/article/details/82355085