Go struct

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 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值