---
title: "golang匿名结构体"
author: "DarkWingBoy"
date: "2014-09-25T18:08:27Z"
tags: []
categories: ["编程语言"]
slug: "golang"
draft: false
lastmod: "2014-09-25T18:08:27Z"
id: 2113223
url: "https://darkwingboy.iteye.com/blog/2113223"
---

```
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)
}
```