go基础-9.结构体

结构体定义

type 结构体名称 struct{
    名称 类型//成员或属性
}

package main

import "fmt"

// Student 定义结构体
type Student struct {
  Name string
  Age  int
}

// PrintInfo 给机构体绑定一个方法
func (s Student) PrintInfo() {
  fmt.Printf("name:%s age:%d\n", s.Name, s.Age)
}

func main() {
  s := Student{
    Name: "枫枫",
    Age:  21,
  }
  s.Name = "枫枫知道" // 修改值
  s.PrintInfo()
}

继承

package main

import "fmt"

type People struct {
  Time string
}

func (p People) Info() {
  fmt.Println("people ", p.Time)
}

// Student 定义结构体
type Student struct {
  People
  Name string
  Age  int
}

// PrintInfo 给机构体绑定一个方法
func (s Student) PrintInfo() {
  fmt.Printf("name:%s age:%d\n", s.Name, s.Age)
}

func main() {
  p := People{
    Time: "2023-11-15 14:51",
  }
  s := Student{
    People: p,
    Name:   "枫枫",
    Age:    21,
  }
  s.Name = "枫枫知道" // 修改值
  s.PrintInfo()
  s.Info()                   // 可以调用父结构体的方法
  fmt.Println(s.People.Time) // 调用父结构体的属性
  fmt.Println(s.Time)        // 也可以这样
}

结构体指针

之前我们了解了值传递和引用传递,如果我想在函数里面或者方法里面修改结构体里面的属性

只能使用结构体指针或者指针方法

package main

import "fmt"

type Student struct {
  Name string
  Age  int
}

func SetAge(info Student, age int) {
  info.Age = age
}

func SetAge1(info *Student, age int) {
  info.Age = age
}

func main() {
  s := Student{
    Name: "枫枫",
    Age:  21,
  }
  fmt.Println(s.Age)
  SetAge(s, 18)
  fmt.Println(s.Age)
  SetAge1(&s, 17)
  fmt.Println(s.Age)
}

package main

import "fmt"

type Student struct {
  Name string
  Age  int
}

func (s Student) SetAge(age int) {
  s.Age = age
}
func (s *Student) SetAge1(age int) {
  s.Age = age
}

func main() {
  s := Student{
    Name: "枫枫",
    Age:  21,
  }
  s.SetAge(18)
  fmt.Println(s.Age)
  s.SetAge1(18)
  fmt.Println(s.Age)
}

结构体tag

package main

import (
  "encoding/json"
  "fmt"
)

type Student struct {
  Name string `json:"name"`
  Age  int    `json:"age"`
}

func main() {
  s := Student{
    Name: "枫枫",
    Age:  21,
  }
  byteData, _ := json.Marshal(s)
  fmt.Println(string(byteData))
}

json tag

  1. 这个不写json标签转换为json的话,字段名就是属性的名字
  2. 小写的属性也不会转换

如果再转json的时候,我不希望某个字段被转出来,我可以写一个 -

package main

import (
  "encoding/json"
  "fmt"
)

type Student struct {
  Name     string `json:"name"`
  Age      int    `json:"age"`
  Password string `json:"-"`
}

func main() {
  s := Student{
    Name:     "枫枫",
    Age:      21,
    Password: "123456",
  }
  byteData, _ := json.Marshal(s)
  fmt.Println(string(byteData)) // {"name":"枫枫","age":21}
}

omitempty

空值省略

package main

import (
  "encoding/json"
  "fmt"
)

type Student struct {
  Name string `json:"name"`
  Age  int    `json:"age,omitempty"`
}

func main() {
  s := Student{
    Name: "枫枫",
    Age:  0, // 空值会被省略
  }
  byteData, _ := json.Marshal(s)
  fmt.Println(string(byteData)) // {"name":"枫枫"}
}

参考文档

结构体 https://mp.weixin.qq.com/s?__biz=Mzg2NTAyNTc5NQ==&mid=2247484449&idx=1&sn=4dc2d55bbdfe20e6b7a68ff711b05fa6&chksm=ce612e4df916a75bbe57d0e1afe9156fcba090410fb4f43fb146bf94b1b0a683fe3381bb5255&scene=27

go语言结构体 https://blog.csdn.net/qq_27870421/article/details/118489821

结构体嵌套 https://blog.csdn.net/demored/article/details/124197637

结构体指针 https://www.runoob.com/note/67933

结构体tag https://c.biancheng.net/view/112.html

posted @ 2024-09-24 11:06  枫枫知道  阅读(3)  评论(0编辑  收藏  举报