Go 的结构体 是各个字段字段的类型的集合。这在组织数据时非常有用

Example:

package main

import "fmt"


type product struct{
    name string
    number int
}


func main(){
    //不指定字段。结构体赋值
    fmt.Println(product{"phone", 10})
    //注意字段名不加引号
    fmt.Println(product{name:"abc", number: 30})

    //省略字段默认为0
    fmt.Println(product{name: "def"})

    //使用点访问结构体属性。
    s := product{name: "yhleng", number: 30}
    fmt.Println(s.name, s.number)
    
    //&生成结构体指针,指针被自动解析引用
    sptr := &s
    fmt.Println(sptr.name)

    sptr.name = "xzdylyh"
    fmt.Println(sptr.name)
}

Result:

$ go run example.go
{phone 10}
{abc 30}
{def 0}
yhleng 30
yhleng
xzdylyh

 

坐标: 上一个例子   下一个例子

 

相关文章:

  • 2021-09-09
  • 2022-02-02
  • 2021-12-19
  • 2021-07-15
  • 2021-05-26
  • 2021-08-13
  • 2022-03-04
  • 2021-07-02
猜你喜欢
  • 2021-08-01
  • 2021-10-29
  • 2022-02-09
  • 2022-12-23
  • 2021-07-06
  • 2021-06-11
  • 2021-09-06
相关资源
相似解决方案