码迷,mamicode.com
首页 > 其他好文 > 详细

structs _ golang

时间:2015-03-14 18:29:19      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:

Go‘s structs are typed collections of fields. They‘re useful for grouping data together to form records

package main

import (
    "fmt"
)

type person struct {
    name string
    age  int
}

func main() {

    fmt.Println(person{"Bob", 20})
    fmt.Println(person{name: "Alice", age: 30})
    fmt.Println(person{name: "Fred"})

    fmt.Println(&person{name: "Ann", age: 40})

    s := person{name: "Sean", age: 50}
    fmt.Println(s.name)

    sp := &s

    fmt.Println(sp.age)

    sp.age = 51

    fmt.Println(sp.age)

}
{Bob 20}
{Alice 30}
{Fred 0}
&{Ann 40}
Sean
50

总结 :

  1 : 通过 & 能得出 struct 的指针

    2 :  You can also use dots with struct pointers - the pointers are automatically dereferenced

structs _ golang

标签:

原文地址:http://www.cnblogs.com/jackkiexu/p/4337927.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!