社区精选|空结构体还能这么用?Go语言的结构体看这篇就够了
type 类型名 struct {
字段名 字段类型
…
}
//示例:
type Animal struct {
Name string
Age int
}
//结构体实例化
//写法1
//var a Animal
//a.Name = "aaa"
//a.Age = 18
//写法2
a := Animal{
Name: "dog",
Age: 18,
}
fmt.Println(fmt.Sprintf("%T - %v", a, a)) //main.Animal - {dog 18}
//结构体指针实例化
//写法1
var b *Animal
b = new(Animal)
//写法2
//b := new(Animal)
//写法3
//b := &Animal{}
b.Name = "cat" //在底层是(*b).Name = "cat",这是Go语言帮我们实现的语法糖
fmt.Println(fmt.Sprintf("%T - %v", b, b)) //*main.Animal - &{cat 0}
var v struct {
Name string
Age int
}
fmt.Println(v)
var v struct{}
fmt.Println(unsafe.Sizeof(v)) //0
v1 := struct{}{}
fmt.Println(unsafe.Sizeof(v1)) //0
func NewPerson(name string, age int8) Person {
return Person{
name: name,
age: age,
}
}
func NewPerson(name string, age int8) *Person {
return &Person{
name: name,
age: age,
sex: sex,
country:country,
province:province,
city:city,
town:town,
address:address,
}
}
func (接收者变量 接收者类型) 方法名(参数列表) (返回参数) {
函数体
}
func NewPerson(name string, age int8) *Person {
return &Person{
name: name,
age: age,
}
}
func (p *Person) Dream() {
p.name = "aaa"
fmt.Printf("%s的梦想是学好Go语言\n", p.name) //aaa的梦想是学好Go语言
}
func main() {
p1 := NewPerson("小王子", 25)
p1.Dream()
fmt.Println(p1) //&{aaa 25}
}
type MyInt int
func (i MyInt) SayInt() {
fmt.Println("my type is MyInt")
}
func main() {
var i1 MyInt
i2 := MyInt(10)
i1.SayInt()
i2.SayInt()
}
输出结果:
my type is MyInt
my type is MyInt
type User struct {
Name string
Gender string
Address //匿名字段
}
type Address struct {
Province string
City string
CreateTime string
}
func main() {
var u1 User
u1.Name = "张三"
u1.Gender = "男"
u1.Address.City = "北京" //匿名字段默认使用类型名作为字段名
u1.CreateTime = "2019" //匿名字段可以省略,但注意多个匿名字段下有相同字段名,会编译失败,所以建议不采用省略写法
fmt.Println(u1)
}
type Animal struct {
Name string
Age int
}
func (a Animal) Say() {
fmt.Println(fmt.Sprintf("1-my name is %s and age is %d", a.Name, a.Age))
}
type Cat struct {
Animal //嵌套结构体实现继承
}
func main() {
c1 := Cat{}
c1.Name = "加菲猫"
c1.Age = 5
c1.Say()
//输出结果:
//1-my name is 加菲猫 and age is 5
}
func (c Cat) Say() {
fmt.Println(fmt.Sprintf("2-my name is %s and age is %d", c.Name, c.Age))
}
func (c Cat) Run() {
fmt.Println(fmt.Sprintf("my name is %s,还是跑步高手", c.Name))
}
func main() {
c1 := Cat{}
c1.Name = "加菲猫"
c1.Age = 5
c1.Say()
c1.Run()
//输出结果:
//2-my name is 加菲猫 and age is 5
//my name is 加菲猫,还是跑步高手
}
`key1:"value1" key2:"value2"`
type CardInfo struct {
Title string `json:"title"`
Desc string
height int `json:"height"`
}
func main() {
c1 := CardInfo{
Title: "成长之星",
Desc: "balabala",
height: 100,
}
data, _ := json.Marshal(c1)
fmt.Println(string(data)) //{"title":"成长之星","Desc":"balabala"}
str := "{"title":"title111", "desc":"desc222", "height":20}"
c2 := CardInfo{}
_ = json.Unmarshal([]byte(str), &c2)
fmt.Println(c2) //{title111 desc222 0}
}
s := struct{}{}
fmt.Println(unsafe.Sizeof(s)) //0
type Set map[int]struct{}
func main() {
s := make(Set)
s.add(1)
s.add(2)
s.add(3)
s.remove(2)
fmt.Println(s.exist(1))
fmt.Println(s)
//输出:
//true
//map[1:{} 3:{}]
}
func (s Set) add(num int) {
s[num] = struct{}{}
}
func (s Set) remove(num int) {
delete(s, num)
}
func (s Set) exist(num int) bool {
_, ok := s[num]
return ok
}
func main() {
ch := make(chan struct{})
go worker(ch)
// Send a message to a worker.
ch <- struct{}{}
// Receive a message from the worker.
<-ch
println("AAA")
//输出:
//BBB
//AAA
}
func worker(ch chan struct{}) {
// Receive a message from the main program.
<-ch
println("BBB")
// Send a message to the main program.
close(ch)
}
type T struct{}
func methodUse() {
t := T{}
t.Print()
t.Print2()
//输出:
//哈哈哈Print
//哈哈哈Print2
}
func (t T) Print() {
fmt.Println("哈哈哈Print")
}
func (t T) Print2() {
fmt.Println("哈哈哈Print2")
}
本文详解了 Go 语言结构体的各种知识点,最后针对空结构体的作用和使用场景,进行了详细的讲解。在之后的实际项目开发过程中,只用占位不用实际含义,那么我们就都可以使用空结构体,可以极大的节省不必要的内存开销。
希望对大家有帮助,兄弟们觉好留赞哦。
关注公众号:拾黑(shiheibook)了解更多
赞助链接:
关注数据与安全,洞悉企业级服务市场:https://www.ijiandao.com/
四季很好,只要有你,文娱排行榜:https://www.yaopaiming.com/
让资讯触达的更精准有趣:https://www.0xu.cn/