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

A Tour of Go Struct Literals

时间:2014-10-27 01:43:25      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   ar   sp   div   on   log   amp   

A struct literal denotes a newly allocated struct value by listing the values of its fields.

You can list just a subset of fields by using the Name: syntax. (And the order of named fields is irrelevant.)

The special prefix & constructs a pointer to a newly allocated struct.

package main 

import "fmt"

type Vertex struct {
    X, Y int
}

var (
    p = Vertex{1, 2} // has type Vertex
    q = &Vertex{1, 2} // has type *Vertex
    r = Vertex{X: 1} // Y:0 is implicit
    s = Vertex{} // X:0 and Y:0
)
func main() {
    fmt.Println(p, q, r, s)
}

 

A Tour of Go Struct Literals

标签:style   blog   color   ar   sp   div   on   log   amp   

原文地址:http://www.cnblogs.com/ghgyj/p/4053302.html

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