ITeye博客

golang匿名结构体

DarkWingBoy · 2014-09-25 18:08 · golang · 编程语言 · Markdown

http://www.dotcoo.com/golang-anonymous-struct
golang匿名结构体
package main
     
import (
    "fmt"
)
     
func main() {
    var user struct{Name string; Gender int}
    user.Name = "dotcoo"
    user.Gender = 1
    fmt.Printf("%#v\n", user)
}
package main

import (
"fmt"
)

func main() {
var user1 struct{Username, Password string}
user1.Username = "dotcoo"
user1.Password = "dotcoopwd"
fmt.Printf("%v\n", user1)

// var user2 struct{Username, Password string} = struct{Username, Password string}{Username:"dotcoo", Password:"dotcoopwd"}
// var user2 = struct{Username, password string}{"dotcoo", "dotcoopwd"}
user2 := struct{Username, password string}{"dotcoo", "dotcoopwd"}
fmt.Printf("%v\n", user2)

// var user3 *struct{Username, Password string}
// user3 = new(struct{Username, Password string})
// var user3 *struct{Username, Password string} = new(struct{Username, Password string})
// var user3 = new(struct{Username, Password string})
user3 := new(struct{Username, Password string})
user3.Username = "dotcoo"
user3.Password = "dotcoopwd"
fmt.Printf("%v\n", user3)

user4 := &struct{Username, Password string}{"dotcoo", "dotcoopwd"}
fmt.Printf("%v\n", user4)
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。