go 语言(九)----struct

  1. 定义一个结构体
type Book struct {
   
	title string
	auth string
}
  1. 结构体使用
package main

import "fmt"

//定义一个结构体
type Book struct {
   
	title string
	auth string
}

func main() {
   

	var book1 Book
	book1.title = "Golang"
	book1.auth = "zhang3"
	
	fmt.Println("%v\n",book1)
}

在这里插入图片描述

  1. 结构体传参
package main

import "fmt"

//定义一个结构体
type Book struct {
   
	title string
	auth string
}

func changeBook(book Book) {
   
	//传递一个book的副本
	book.auth = "666"
}

func changeBook3(book *Book)  {
   
	//指针传递
	book.auth = "777"

}

func main() {
   

	var book1 Book
	book1.title = "Golang"
	book1.auth = "zhang3"

	fmt.Println("%v\n",book1)

	//副本传递是不会改变结构体的值
	changeBook(book1)
	fmt.Println("副本传递",book1)

	//指针传递
	changeBook3(&book1)
	fmt.Println("指针传递",book1)
}

在这里插入图片描述

相关推荐

  1. GO语言基础笔记():工程实践

    2024-01-20 09:38:04       55 阅读
  2. go |struct embedding、generics、goroutine

    2024-01-20 09:38:04       44 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-20 09:38:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-20 09:38:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-20 09:38:04       87 阅读
  4. Python语言-面向对象

    2024-01-20 09:38:04       96 阅读

热门阅读

  1. 【Go】A和*A在作为Receiver和接口实现上的差别

    2024-01-20 09:38:04       53 阅读
  2. JVM与HotSpot

    2024-01-20 09:38:04       52 阅读
  3. Docker部署微服务问题及解决

    2024-01-20 09:38:04       50 阅读
  4. var 和 let 的优缺点

    2024-01-20 09:38:04       59 阅读
  5. python爬虫如何写,有哪些成功爬取的案例

    2024-01-20 09:38:04       49 阅读
  6. mysql怎么开启一个事务

    2024-01-20 09:38:04       62 阅读
  7. 344. 观光之旅(最小环问题,Floyd)

    2024-01-20 09:38:04       57 阅读
  8. Docker的安装和使用

    2024-01-20 09:38:04       48 阅读
  9. Redis多线程模型探究

    2024-01-20 09:38:04       38 阅读