The struct structure of Go language

Source: Internet
Author: User

struct structure
    • The struct in Go is very similar to the struct in C, and go has no class
    • Define structure using type<name> struct{}, name follows visibility rules
    • Supports pointer-type members pointing to themselves
    • Supports anonymous structures that can be used as members or define member variables
    • Anonymous structures can also be used for map values
    • Structure can be initialized with literal values
    • Allows you to read and write struct members directly through pointers
    • Members of the same type can make direct copy assignment
    • The = = and! = Comparison operators are supported, but not > or <
    • Support for anonymous fields, essentially a field defined by a type name
    • Embedded structure as anonymous field looks like inheritance, but not inherited
    • You can use the anonymous field pointer
Test Cases
//package mainimport "fmt"//定义了一 个空的结构体type st01 struct {}//定义一个非空的结构体type person struct {    Name string    Age  int}func main() {    personInfo := person{}    personInfo.Age = 19    personInfo.Name = "xiaoqiang"    fmt.Println(personInfo)    //------------------------------    fmt.Println("=============================")    //直接初始化   属于 字面值 初始化    personInfo2 := person{Name :"spark01", Age: 19}    fmt.Println(personInfo2)}
struct, passed by value

//结构体,按值 传递package mainimport "fmt"//定义一个类型type sparkInfo struct {    SparkClusterName string    SparkNodeNum     int}func main() {    newSparkInfo := sparkInfo{        SparkNodeNum:     8,        SparkClusterName: "spark-test-001"}    fmt.Println("main:\t", newSparkInfo)    updateSparkInfo(newSparkInfo)    fmt.Println("main:\t", newSparkInfo)}//同样,这里是值传递,内部的修改,并不会影响到 旧值的func updateSparkInfo(sparkInfo sparkInfo) {    sparkInfo.SparkNodeNum = 9    fmt.Println("updateSparkInfo:\t", sparkInfo)}
Structure, passing by address

//结构体,按地址传递package mainimport "fmt"type k8sInfo struct {    K8sClusterName string //k8s集群的名称    K8sClusterNumm int    //k8s集群的节点个数}func main() {    k8sInfo := k8sInfo{        K8sClusterNumm: 1,        K8sClusterName: "k8s-test-001"}    fmt.Println(k8sInfo)    updateK8sClusterInfo(&k8sInfo)    fmt.Println(k8sInfo)}//传递的是 地址,按地址传递//修改了,旧的值func updateK8sClusterInfo(info *k8sInfo) {    info.K8sClusterNumm = 110    fmt.Println("updateK8s:\t", info)}
Structure, passing by address

//结构体,按地址传递package mainimport "fmt"type dockerInfo struct {    DockerClusterName string    DockerClusterNum int}func main() {    //一般更习惯这种写法,    //在初始化的时候,就直接获取地址    //这样以后在调用的时候,就不需要添加&号了    dInfo := &dockerInfo{        DockerClusterNum:19,        DockerClusterName:"docker-yizhuang"}    fmt.Println("init docker:\t", dInfo)    updateDockerInfo(dInfo)    fmt.Println("after docker:\t", *dInfo)}func updateDockerInfo (info *dockerInfo) {    info.DockerClusterNum = 80    fmt.Println("udpateDocker:\t", info)}
Anonymous structure

//匿名结构package mainimport "fmt"func main() {    //创建一个匿名结构,    //并且进行了初始化    //而且,直接获取地址&    ftp := &struct {        FtpName string        FtpNum int    }{        FtpName:"ftp-beijing",        FtpNum:8}    fmt.Println(ftp)    fmt.Println(*ftp)    fmt.Println("FtpName:\t", ftp.FtpName)    fmt.Println("FtpNum:\t", ftp.FtpNum)}
Anonymous structure, nested into other structures

//将匿名结构,嵌套进别的结构体里package mainimport "fmt"type hadoop struct {    HadoopClusterName string    HadoopClusterNum  int    //创建一个匿名结构    HadoopOtherInfo struct {        //同样,当多个变量都一样的时候,也可用省略        //这是Go语言的优点        HadoopVersion, HadoopUrl string    }}func main() {    hdfs := &hadoop{        HadoopClusterName: "Hadoop-test-001",        HadoopClusterNum:  9}    //只能通过这种方式,进行初始化    hdfs.HadoopOtherInfo.HadoopUrl = "http://192.168.1.110:50070"    hdfs.HadoopOtherInfo.HadoopVersion = "v2.7.0"    fmt.Println(hdfs)    fmt.Println(*hdfs)    fmt.Println("HadoopClusterName:\t", hdfs.HadoopClusterName)    fmt.Println("HadoopClusterNum:\t", hdfs.HadoopClusterNum)    fmt.Println("HadoopClusterVersion:\t", hdfs.HadoopOtherInfo.HadoopVersion)    fmt.Println("HadoopClusterUrl:\t", hdfs.HadoopOtherInfo.HadoopUrl)}
Anonymous fields

//匿名字段  测试package mainimport "fmt"type students struct {    //这些就是匿名字段,没有定义名字    string    int}func main() {    boy := &students{        //初始化的时候,必须按照顺序来进行的        "xiaoqiang", 19}    fmt.Println(boy)}
Operations between identical structures, such as assignment, comparison

// 相同结构体  之间 操作,如赋值,比较package mainimport "fmt"type teacherA struct {    string    int} type teacherB struct {    string    int }func main() {    boyTeacherA := teacherA{"xiaoli",22}    //boyTeacherB := teacherB{"xiaoli",22}    //説明:编译报错了,teacherA, teacherB  类型不相同,不能进行比较的    //fmt.Println(boyTeacherA == boyTeacherB)    boyTeacherB := teacherA{"xiaoli", 23}    fmt.Println(boyTeacherB == boyTeacherA)}
struct, implementing an effect similar to inheritance

//结构体,实现 类似于 继承的效果package mainimport "fmt"type anminal struct {    //设置一些共有的属性    Name, address string}type cat struct {    //anminal  Go 语言,默认,anminal是类型,同时也是属性名称    anminal    Sex int         // 猫的特有属性,性别是啥}type dog struct {    anminal    Hobby string  //狗的特有属性,爱好}func main() {    //第一种初始化方式    xiaoCat := cat{Sex:0, anminal : anminal{Name:"xiaohong", address:"beijing"}}    xiaoDog := dog{Hobby:"play", anminal:anminal{Name:"xiaohuang", address:"shanghai"}}    //第二种初始化方式    xiaoCat.anminal.Name = "xiaoxiaoxiaohong" //这种方式,是为了防止名字相同时,冲突    xiaoCat.Name = "xiaoxiaohong"    fmt.Println("cat:\t", xiaoCat)    fmt.Println("dog:\t",xiaoDog)}

The struct structure of Go language

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.