struct

248 字
1 分钟
struct

struct#

// 声明类型为myint的新数据类型,int的别名
type myint int
// 定义结构体
type Book struct {
title string
author string
}
func main() {
var a myint = 10
fmt.Printf("a=%v, type of a=%T\n", a, a)
var book1 Book
book1.title = "Golang"
book1.author = "Shai"
printBook(book1)
fmt.Printf("printBook-book1=%v\n", book1)
changeBook(book1)
fmt.Printf("changeBook-book1=%v\n", book1)
changeBook1(&book1)
fmt.Printf("changeBook1-book1=%v\n", book1)
}
func printBook(book Book) {
book.title = "printBookGolang"
fmt.Printf("book=%v\n", book)
}
func changeBook(book Book) {
book.title = "ChangeGolang"
}
func changeBook1(book *Book) {
book.title = "PointerGolang"
}
-------------
a=10, type of a=main.myint
book={printBookGolang Shai}
printBook-book1={Golang Shai}
changeBook-book1={Golang Shai}
changeBook1-book1={PointerGolang Shai}
type Hero struct {
// 首字母大写,表示该属性是对外能够访问的
Name string
Ad int
// 首字母小谢,只能类的内部访问
level int
}
// Show方法绑定到Hero结构体
func (this Hero) Show() {
fmt.Println("Name = ", this.Name)
fmt.Println("Ad = ", this.Ad)
fmt.Println("Level = ", this.level)
}
func (this *Hero) GetLevel() int {
return this.level
}
func (this *Hero) SetName(newName string) {
fmt.Println("SetName = ", newName)
this.Name = newName
}
func main() {
hero := Hero{Name: "Superman", Ad: 100, level: 999}
hero.Show()
fmt.Println("Hero's Level is ", hero.GetLevel())
hero.SetName("Zhangsan")
hero.Show()
}
-----------------
Name = Superman
Ad = 100
Level = 999
Hero's Level is 999
SetName = Zhangsan
Name = Zhangsan
Ad = 100
Level = 999

文章分享

如果这篇文章对你有帮助,欢迎分享给更多人!

struct
https://shaio.top/posts/丫丫学语/golang/struct/
作者
沙海
发布于
2026-08-10
许可协议
CC BY-NC-SA 4.0

评论区

Profile Image of the Author
沙海
Hello, I'm Shai.
公告
欢迎来到我的博客!这是一则示例公告。
音乐
封面

音乐

暂未播放

0:00 0:00
暂无歌词
分类
标签
站点统计
文章
84
分类
15
标签
48
总字数
67,035
运行时长
0 天
最后活动
0 天前
站点信息
构建平台
GitHub Actions
博客版本
Firefly v6.12.2
文章许可
CC BY-NC-SA 4.0

文章目录