golang匿名结构体

2012-06-27 17:24  5481人阅读  评论 (0)
Tags: golang

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)
}
豫ICP备09035262号-1