美文网首页 技术博客
go语言的结构体(struct)

go语言的结构体(struct)

作者: 老生住长亭 | 来源:发表于2019-03-03 23:32 被阅读0次

简单介绍下结构体struct:

type name struct {
xx1 类型
xx2 类型
...
}

package main

import "fmt"

type user struct {
        name string
        email string
        age int
        privileged bool
}

func  (u user) notify () {
        fmt.Printf("Send email to %s <%s>\n", u.name, u.email)
}

func (u *user)changeEmail(email string) {
        u.email = email
}

func main () {
        // struct 定义变量时,不能少字段
        lisa := user{"tom", "xx@gmail.com", 21, true}
        lisa.notify()
      
      // 使用user的指针赋值
        bill := &user{"lisa", "sss@gmail.com", 43, false}
        bill.notify()

        lisa.changeEmail("5423131@gmail.com")
        lisa.notify()
}

结果:

go build struct.go

go run struct.go

运行结果:

Send email to tom <xx@gmail.com>
Send email to lisa <sss@gmail.com>
Send email to tom <5423131@gmail.com>

相关文章

  • 《Go语言四十二章经》第十八章 Struct 结构体

    《Go语言四十二章经》第十八章 Struct 结构体 作者:李骁 18.1结构体(struct) Go 通过结构体...

  • golang继承与接口

    继承 结构体 Go语言的结构体(struct)和其他语言的类(class)有同等的地位,但Go语言放弃了包括继 承...

  • Go 语言结构体(struct)

    定义数组可以存储同一类型的数据,但在结构体中我们可以为不同项定义不同的数据类型。 结构体是由一系列具有相同类型或不...

  • GO语言学习

    go语言的结构体 声明:type 结构体名 struct{x,y int}初始化:未显示初始化结构体变量的,初始值...

  • go面向对象-初始化

    Go语言的结构体(struct)和其他语言的类(class)有同等的地位,但Go语言放弃了包括继承在内的大量面向对...

  • go语言的结构体(struct)

    简单介绍下结构体struct: type name struct {xx1 类型xx2 类型...} 结果: go...

  • Go语言之结构体嵌套

    Go语言之结构体嵌套 在type Student2 struct结构体中,注意book参数引用的是地址,方便修改数据

  • 【OC梳理】结构体、枚举

    结构体(struct) OC中的结构体(struct),其实就是C语言中的结构体(struct)常见使用方法。OC...

  • Go 面向对象、接口(二)

    欢迎来我的博客 go 语言仅支持封装,不支持继承和多态go 语言没有class 只有struct 结构体 声明和创...

  • Go语言面向对象三大特性—继承

    Go语言里靠匿名结构体实现继承,嵌入到新的结构体里面。如果一个struct嵌套了另一个匿名结构体,那么这个结构体可...

网友评论

    本文标题:go语言的结构体(struct)

    本文链接:https://www.haomeiwen.com/subject/btuvuqtx.html