Go fast and simple language entry (c)

First, the structure

  1. definition
type 类型名 struct {
    字段1 字段1类型
    字段2 字段2类型
    …
}

The same type can be abbreviated

type Color struct {
    R, G, B byte
}
  1. Examples of defined structure & calls
  • Basic types of calls
type Point struct {
    X int
    Y int
}
var p Point
p.X = 10
p.Y = 20
  • Pointer types of calls
type Player struct{
    Name string
    HealthPoint int
    MagicPoint int
}
tank := new(Player)
tank.Name = "Canon"   //语法糖,将 tank.Name 形式转换为 (*tank).Name
tank.HealthPoint = 300
type Command struct {
    Name    string    // 指令名称
    Var     *int    // 指令绑定的变量
    Comment string    // 指令的注释
}
var version int = 1
cmd := &Command{}
cmd.Name = "version"
cmd.Var = &version
cmd.Comment = "show version"
  1. Structure initialization
  • Key to filling
ins := 结构体类型名{
    字段1: 字段1的值,
    字段2: 字段2的值,
    …
}
type People struct {
    name  string
    child *People
}
relation := &People{
    name: "爷爷",
    child: &People{
        name: "爸爸",
        child: &People{
                name: "我",
        },
    },
}
  • List initialize multiple values
ins := 结构体类型名{
    字段1的值,
    字段2的值,
    …
}
type Address struct {
    Province    string
    City        string
    ZipCode     int
    PhoneNumber string
}
addr := Address{
    "四川",
    "成都",
    610000,
    "0",
}
fmt.Println(addr)
  • Initialization anonymous structure
ins := struct {
    // 匿名结构体字段定义
    字段1 字段类型1
    字段2 字段类型2
    …
}{
    // 字段值初始化
    初始化字段1: 字段1的值,
    初始化字段2: 字段2的值,
    …
}
ins := struct {
    字段1字段类型1
    字段2字段类型2
    …
}
package main
import (
    "fmt"
)
// 打印消息类型, 传入匿名结构体
func printMsgType(msg *struct {
    id   int
    data string
}) {
    // 使用动词%T打印msg的类型
    fmt.Printf("%T\n", msg)
}
func main() {
    // 实例化一个匿名结构体
    msg := &struct {  // 定义部分
        id   int
        data string
    }{  // 值初始化部分
        1024,
        "hello",
    }
    printMsgType(msg)
}
  1. Constructor
  • Analog overloaded constructor
type Cat struct {
    Color string
    Name  string
}
func NewCatByName(name string) *Cat {
    return &Cat{
        Name: name,
    }
}
func NewCatByColor(color string) *Cat {
    return &Cat{
        Color: color,
    }
}
  • Analog parent constructor call
type Cat struct {
    Color string
    Name  string
}
type BlackCat struct {
    Cat  // 嵌入Cat, 类似于派生
}
// “构造基类”
func NewCat(name string) *Cat {
    return &Cat{
        Name: name,
    }
}
// “构造子类”
func NewBlackCat(color string) *BlackCat {
    cat := &BlackCat{}
    cat.Color = color
    return cat
}
  1. The method and receiver
  • Method structure
    (it was like Example Mode)
type Bag struct {
    items []int
}
func (b *Bag) Insert(itemid int) {
    b.items = append(b.items, itemid)
}
func main() {
    b := new(Bag)
    b.Insert(1001)
}
  • The receiver
    is actually an instance of the example above, b * Bag, two types of the receiver, the pointer and non-pointer type type.

    Pointer type receiver, when calling the method, modifying the received pointer variable to any member, after the method, the modifications are effective.

package main
import "fmt"
// 定义属性结构
type Property struct {
    value int  // 属性值
}
// 设置属性值
func (p *Property) SetValue(v int) {
    // 修改p的成员变量
    p.value = v
}
// 取属性值
func (p *Property) Value() int {
    return p.value
}
func main() {
    // 实例化属性
    p := new(Property)
    // 设置值
    p.SetValue(100)
    // 打印值
    fmt.Println(p.Value())
}

Non-pointer type of the receiver, the receiver may obtain the value of the member, but modified invalid.

package main
import (
    "fmt"
)
// 定义点结构
type Point struct {
    X int
    Y int
}
// 非指针接收器的加方法
func (p Point) Add(other Point) Point {
    // 成员值与参数相加后返回新的结构
    return Point{p.X + other.X, p.Y + other.Y}
}
func main() {
    // 初始化点
    p1 := Point{1, 1}
    p2 := Point{2, 2}
    // 与另外一个点相加
    result := p1.Add(p2)
    // 输出结果
    fmt.Println(result)    //{3,3}
}
  1. to be continue

Guess you like

Origin blog.csdn.net/LiuYangQ_Q/article/details/90930204