package main
import (
"fmt"
)
// 一、结构体声明
// 成员变量必须是大驼峰,不然外部无法访问
type Employee struct {
ID int // 0 -> 2
Name string // ""
Address string // ""
Position string // "" -> "boss"
Salary int // 0 -> 5000
ManagerID int // 0
}
type Books struct {
title string
author string
subject string
book_id int
}
func main() {
// 二、定义结构体变量
var employee Employee
// 三、初始化
// 创建一个新的结构体
fmt.Println(Books{
"Go 语言", "author1", "Go 语言教程", 2})
// 也可以使用 key => value 格式
fmt.Println(Books{
title: "Go 语言", author: "author2", subject: "Go 语言教程", book_id: 1})
// 忽略的字段为 0 或 空
fmt.Println(Books{
title: "Go 语言", author: "author3"})
// 四、结构体变量的成员可以通过点操作符访问
employee.Salary = 5000
// 或者对成员取地址通过指针访问
position := &employee.Position
Go struct
最新推荐文章于 2022-04-10 15:15:01 发布