golang学习之struct
caiyezi · · 1301 次点击 · · 开始浏览结构体定义的一般方式如下:
type identifier struct { field1 type1 field2 type2 ... }
type T struct {a, b int} 也是合法的语法,它更适用于简单的结构体。
var t *T t = new(T)
变量 t 是一个指向 T的指针,此时结构体字段的值是它们所属类型的零值,使用 new 函数给一个新的结构体变量分配内存,它返回指向已分配内存的指针。
无论变量是一个结构体类型还是一个结构体类型指针,都使用同样的 选择器符(selector-notation) 来引用结构体的字段,即:
type myStruct struct { i int } var v myStruct // v是结构体类型变量
var p *myStruct // p是指向一个结构体类型变量的指针
v.i p.i
struct实例:
package main import ( "fmt"
"strings" ) type Person struct { firstName string lastName string } func upPerson(p *Person) { p.firstName = strings.ToUpper(p.firstName) p.lastName = strings.ToUpper(p.lastName) } func main() { // 1-struct as a value type:
var pers1 Person pers1.firstName = "Chris" pers1.lastName = "Woodward" upPerson(&pers1) fmt.Printf("The name of the person is %s %s\n", pers1.firstName, pers1.lastName) // 2—struct as a pointer:
pers2 := new(Person) pers2.firstName = "Chris" pers2.lastName = "Woodward" (*pers2).lastName = "Woodward" // 这是合法的
upPerson(pers2) fmt.Printf("The name of the person is %s %s\n", pers2.firstName, pers2.lastName) // 3—struct as a literal:
pers3 := &Person{"Chris", "Woodward"} upPerson(pers3) fmt.Printf("The name of the person is %s %s\n", pers3.firstName, pers3.lastName) }
程序输出:
The name of the person is CHRIS WOODWARD The name of the person is CHRIS WOODWARD The name of the person is CHRIS WOODWARD
使用反射访问struct的tag标签:
package main import ( "fmt"
"reflect" ) type Person struct { firstName string `An important answer` lastName string `The name of the thing` } func main() { tt := Person{"first", "last"} for i := 0; i < 2; i++ { refTag(tt, i) } }
程序输出:
Tag:An important answer Name:firstName Tag:The name of the thing Name:lastName
在值和指针上调用方法:
可以有连接到类型的方法,也可以有连接到类型指针的方法。
但是这没关系:对于类型 T,如果在 *T 上存在方法 Meth(),并且 t 是这个类型的变量,那么 t.Meth() 会被自动转换为(&t).Meth()。
指针方法和值方法都可以在指针或非指针上被调用,收者类型是 *TwoInts 的方法 AddThem(),它能在类型 TwoInts 的值上被调用,这是自动间接发生的。
因此 two2.AddThem 可以替代 (&two2).AddThem()。
再说一句,go中有方法继承与覆盖,不过必须是匿名字段o
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
结构体定义的一般方式如下:
type identifier struct { field1 type1 field2 type2 ... }
type T struct {a, b int} 也是合法的语法,它更适用于简单的结构体。
var t *T t = new(T)
变量 t 是一个指向 T的指针,此时结构体字段的值是它们所属类型的零值,使用 new 函数给一个新的结构体变量分配内存,它返回指向已分配内存的指针。
无论变量是一个结构体类型还是一个结构体类型指针,都使用同样的 选择器符(selector-notation) 来引用结构体的字段,即:
type myStruct struct { i int } var v myStruct // v是结构体类型变量
var p *myStruct // p是指向一个结构体类型变量的指针
v.i p.i
struct实例:
package main import ( "fmt"
"strings" ) type Person struct { firstName string lastName string } func upPerson(p *Person) { p.firstName = strings.ToUpper(p.firstName) p.lastName = strings.ToUpper(p.lastName) } func main() { // 1-struct as a value type:
var pers1 Person pers1.firstName = "Chris" pers1.lastName = "Woodward" upPerson(&pers1) fmt.Printf("The name of the person is %s %s\n", pers1.firstName, pers1.lastName) // 2—struct as a pointer:
pers2 := new(Person) pers2.firstName = "Chris" pers2.lastName = "Woodward" (*pers2).lastName = "Woodward" // 这是合法的
upPerson(pers2) fmt.Printf("The name of the person is %s %s\n", pers2.firstName, pers2.lastName) // 3—struct as a literal:
pers3 := &Person{"Chris", "Woodward"} upPerson(pers3) fmt.Printf("The name of the person is %s %s\n", pers3.firstName, pers3.lastName) }
程序输出:
The name of the person is CHRIS WOODWARD The name of the person is CHRIS WOODWARD The name of the person is CHRIS WOODWARD
使用反射访问struct的tag标签:
package main import ( "fmt"
"reflect" ) type Person struct { firstName string `An important answer` lastName string `The name of the thing` } func main() { tt := Person{"first", "last"} for i := 0; i < 2; i++ { refTag(tt, i) } }
程序输出:
Tag:An important answer Name:firstName Tag:The name of the thing Name:lastName
在值和指针上调用方法:
可以有连接到类型的方法,也可以有连接到类型指针的方法。
但是这没关系:对于类型 T,如果在 *T 上存在方法 Meth(),并且 t 是这个类型的变量,那么 t.Meth() 会被自动转换为(&t).Meth()。
指针方法和值方法都可以在指针或非指针上被调用,收者类型是 *TwoInts 的方法 AddThem(),它能在类型 TwoInts 的值上被调用,这是自动间接发生的。
因此 two2.AddThem 可以替代 (&two2).AddThem()。
再说一句,go中有方法继承与覆盖,不过必须是匿名字段o