18.4 結構體

創建:

type struct1 struct {
    field1 type1
    field2 type2
    …
}
ms := new(struct1)

初始化:

ms := &struct1{10, 15.5, "Chris"}

當結構體的命名以大寫字母開頭時,該結構體在包外可見。 通常情況下,爲每個結構體定義一個構建函數,並推薦使用構建函數初始化結構體(參考例10.2):

ms := Newstruct1{10, 15.5, "Chris"}
func Newstruct1(n int, f float32, name string) *struct1 {
    return &struct1{n, f, name} 
}

鏈接

results matching ""

    No results matching ""