2022-08-04 06:59:33 +00:00
<!DOCTYPE HTML>
< html lang = "zh" class = "sidebar-visible no-js light" >
< head >
<!-- Book generated using mdBook -->
< meta charset = "UTF-8" >
< title > 结构体 - Go语言圣经< / title >
<!-- Custom HTML head -->
< meta content = "text/html; charset=utf-8" http-equiv = "Content-Type" >
< meta name = "description" content = "<The Go Programming Language>中文版" >
< meta name = "viewport" content = "width=device-width, initial-scale=1" >
< meta name = "theme-color" content = "#ffffff" / >
< link rel = "icon" href = "../favicon.svg" >
< link rel = "shortcut icon" href = "../favicon.png" >
< link rel = "stylesheet" href = "../css/variables.css" >
< link rel = "stylesheet" href = "../css/general.css" >
< link rel = "stylesheet" href = "../css/chrome.css" >
< link rel = "stylesheet" href = "../css/print.css" media = "print" >
<!-- Fonts -->
< link rel = "stylesheet" href = "../FontAwesome/css/font-awesome.css" >
< link rel = "stylesheet" href = "../fonts/fonts.css" >
<!-- Highlight.js Stylesheets -->
< link rel = "stylesheet" href = "../highlight.css" >
< link rel = "stylesheet" href = "../tomorrow-night.css" >
< link rel = "stylesheet" href = "../ayu-highlight.css" >
<!-- Custom theme stylesheets -->
< link rel = "stylesheet" href = "../style.css" >
< / head >
< body >
<!-- Provide site root to javascript -->
< script type = "text/javascript" >
var path_to_root = "../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "navy" : "light";
< / script >
<!-- Work around some values being stored in localStorage wrapped in quotes -->
< script type = "text/javascript" >
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') & & theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') & & sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
< / script >
<!-- Set the theme before any content is loaded, prevents flash -->
< script type = "text/javascript" >
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
var html = document.querySelector('html');
html.classList.remove('no-js')
html.classList.remove('light')
html.classList.add(theme);
html.classList.add('js');
< / script >
<!-- Hide / unhide sidebar before it is displayed -->
< script type = "text/javascript" >
var html = document.querySelector('html');
var sidebar = 'hidden';
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
}
html.classList.remove('sidebar-visible');
html.classList.add("sidebar-" + sidebar);
< / script >
< nav id = "sidebar" class = "sidebar" aria-label = "Table of contents" >
< div class = "sidebar-scrollbox" >
2023-03-31 10:23:29 +00:00
< ol class = "chapter" > < li class = "chapter-item expanded affix " > < a href = "../index.html" > Go语言圣经< / a > < / li > < li class = "chapter-item expanded affix " > < a href = "../preface.html" > 前言< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch1/ch1.html" > < strong aria-hidden = "true" > 1.< / strong > 入门< / a > < / li > < li > < ol class = "section" > < li class = "chapter-item expanded " > < a href = "../ch1/ch1-01.html" > < strong aria-hidden = "true" > 1.1.< / strong > Hello, World< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch1/ch1-02.html" > < strong aria-hidden = "true" > 1.2.< / strong > 命令行参数< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch1/ch1-03.html" > < strong aria-hidden = "true" > 1.3.< / strong > 查找重复的行< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch1/ch1-04.html" > < strong aria-hidden = "true" > 1.4.< / strong > GIF动画< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch1/ch1-05.html" > < strong aria-hidden = "true" > 1.5.< / strong > 获取URL< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch1/ch1-06.html" > < strong aria-hidden = "true" > 1.6.< / strong > 并发获取多个URL< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch1/ch1-07.html" > < strong aria-hidden = "true" > 1.7.< / strong > Web服务< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch1/ch1-08.html" > < strong aria-hidden = "true" > 1.8.< / strong > 本章要点< / a > < / li > < / ol > < / li > < li class = "chapter-item expanded " > < a href = "../ch2/ch2.html" > < strong aria-hidden = "true" > 2.< / strong > 程序结构< / a > < / li > < li > < ol class = "section" > < li class = "chapter-item expanded " > < a href = "../ch2/ch2-01.html" > < strong aria-hidden = "true" > 2.1.< / strong > 命名< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch2/ch2-02.html" > < strong aria-hidden = "true" > 2.2.< / strong > 声明< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch2/ch2-03.html" > < strong aria-hidden = "true" > 2.3.< / strong > 变量< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch2/ch2-04.html" > < strong aria-hidden = "true" > 2.4.< / strong > 赋值< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch2/ch2-05.html" > < strong aria-hidden = "true" > 2.5.< / strong > 类型< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch2/ch2-06.html" > < strong aria-hidden = "true" > 2.6.< / strong > 包和文件< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch2/ch2-07.html" > < strong aria-hidden = "true" > 2.7.< / strong > 作用域< / a > < / li > < / ol > < / li > < li class = "chapter-item expanded " > < a href = "../ch3/ch3.html" > < strong aria-hidden = "true" > 3.< / strong > 基础数据类型< / a > < / li > < li > < ol class = "section" > < li class = "chapter-item expanded " > < a href = "../ch3/ch3-01.html" > < strong aria-hidden = "true" > 3.1.< / strong > 整型< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch3/ch3-02.html" > < strong aria-hidden = "true" > 3.2.< / strong > 浮点数< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch3/ch3-03.html" > < strong aria-hidden = "true" > 3.3.< / strong > 复数< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch3/ch3-04.html" > < strong aria-hidden = "true" > 3.4.< / strong > 布尔型< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch3/ch3-05.html" > < strong aria-hidden = "true" > 3.5.< / strong > 字符串< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch3/ch3-06.html" > < strong aria-hidden = "true" > 3.6.< / strong > 常量< / a > < / li > < / ol > < / li > < li class = "chapter-item expanded " > < a href = "../ch4/ch4.html" > < strong aria-hidden = "true" > 4.< / strong > 复合数据类型< / a > < / li > < li > < ol class = "section" > < li class = "chapter-item expanded " > < a href = "../ch4/ch4-01.html" > < strong aria-hidden = "true" > 4.1.< / strong > 数组< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch4/ch4-02.html" > < strong aria-hidden = "true" > 4.2.< / strong > Slice< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch4/ch4-03.html" > < strong aria-hidden = "true" > 4.3.< / strong > Map< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch4/ch4-04.html" class = "active" > < strong aria-hidden = "true" > 4.4.< / strong > 结构体< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch4/ch4-05.html" > < strong aria-hidden = "true" > 4.5.< / strong > JSON< / a > < / li > < li class = "chapter-item expanded " > < a href = "../ch4/ch4-06.html" > < s
2022-08-04 06:59:33 +00:00
< div id = "sidebar-resize-handle" class = "sidebar-resize-handle" > < / div >
< / nav >
< div id = "page-wrapper" class = "page-wrapper" >
< div class = "page" >
< div id = "menu-bar-hover-placeholder" > < / div >
< div id = "menu-bar" class = "menu-bar sticky bordered" >
< div class = "left-buttons" >
< button id = "sidebar-toggle" class = "icon-button" type = "button" title = "Toggle Table of Contents" aria-label = "Toggle Table of Contents" aria-controls = "sidebar" >
< i class = "fa fa-bars" > < / i >
< / button >
< button id = "theme-toggle" class = "icon-button" type = "button" title = "Change theme" aria-label = "Change theme" aria-haspopup = "true" aria-expanded = "false" aria-controls = "theme-list" >
< i class = "fa fa-paint-brush" > < / i >
< / button >
< ul id = "theme-list" class = "theme-popup" aria-label = "Themes" role = "menu" >
< li role = "none" > < button role = "menuitem" class = "theme" id = "light" > Light (default)< / button > < / li >
< li role = "none" > < button role = "menuitem" class = "theme" id = "rust" > Rust< / button > < / li >
< li role = "none" > < button role = "menuitem" class = "theme" id = "coal" > Coal< / button > < / li >
< li role = "none" > < button role = "menuitem" class = "theme" id = "navy" > Navy< / button > < / li >
< li role = "none" > < button role = "menuitem" class = "theme" id = "ayu" > Ayu< / button > < / li >
< / ul >
< button id = "search-toggle" class = "icon-button" type = "button" title = "Search. (Shortkey: s)" aria-label = "Toggle Searchbar" aria-expanded = "false" aria-keyshortcuts = "S" aria-controls = "searchbar" >
< i class = "fa fa-search" > < / i >
< / button >
< / div >
< h1 class = "menu-title" > Go语言圣经< / h1 >
< div class = "right-buttons" >
< a href = "../print.html" title = "Print this book" aria-label = "Print this book" >
< i id = "print-button" class = "fa fa-print" > < / i >
< / a >
< a href = "https://github.com/gopl-zh/gopl-zh.github.com" title = "Git repository" aria-label = "Git repository" >
< i id = "git-repository-button" class = "fa fa-github" > < / i >
< / a >
< a href = "https://github.com/gopl-zh/gopl-zh.github.com/edit/master/./ch4/ch4-04.md" title = "Suggest an edit" aria-label = "Suggest an edit" >
< i id = "git-edit-button" class = "fa fa-edit" > < / i >
< / a >
< / div >
< / div >
< div id = "search-wrapper" class = "hidden" >
< form id = "searchbar-outer" class = "searchbar-outer" >
< input type = "search" id = "searchbar" name = "searchbar" placeholder = "Search this book ..." aria-controls = "searchresults-outer" aria-describedby = "searchresults-header" >
< / form >
< div id = "searchresults-outer" class = "searchresults-outer hidden" >
< div id = "searchresults-header" class = "searchresults-header" > < / div >
< ul id = "searchresults" >
< / ul >
< / div >
< / div >
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
< script type = "text/javascript" >
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
< / script >
< div id = "content" class = "content" >
<!-- Page table of contents -->
< div class = "sidetoc" > < nav class = "pagetoc" > < / nav > < / div >
< main >
<!-- 头部 -->
< ul dir = "auto" >
2022-08-24 10:48:15 +00:00
< li > < em > KusonStack一站式可编程配置技术栈(Go): < a href = "https://github.com/KusionStack/kusion" > https://github.com/KusionStack/kusion< / a > < / em > < / li >
< li > < em > KCL 配置编程语言(Rust): < a href = "https://github.com/KusionStack/KCLVM" > https://github.com/KusionStack/KCLVM< / a > < / em > < / li >
< li > < em > 凹语言™: < a href = "https://github.com/wa-lang/wa" > https://github.com/wa-lang/wa< / a > < / em > < / li >
2022-08-04 06:59:33 +00:00
< / ul >
< hr >
< h2 id = "44-结构体" > < a class = "header" href = "#44-结构体" > 4.4. 结构体< / a > < / h2 >
< p > 结构体是一种聚合的数据类型,是由零个或多个任意类型的值聚合成的实体。每个值称为结构体的成员。用结构体的经典案例是处理公司的员工信息,每个员工信息包含一个唯一的员工编号、员工的名字、家庭住址、出生日期、工作岗位、薪资、上级领导等等。所有的这些信息都需要绑定到一个实体中,可以作为一个整体单元被复制,作为函数的参数或返回值,或者是被存储到数组中,等等。< / p >
< p > 下面两个语句声明了一个叫Employee的命名的结构体类型, 并且声明了一个Employee类型的变量dilbert: < / p >
< pre > < code class = "language-Go" > type Employee struct {
ID int
Name string
Address string
DoB time.Time
Position string
Salary int
ManagerID int
}
var dilbert Employee
< / code > < / pre >
< p > dilbert结构体变量的成员可以通过点操作符访问, 比如dilbert.Name和dilbert.DoB。因为dilbert是一个变量, 它所有的成员也同样是变量, 我们可以直接对每个成员赋值: < / p >
< pre > < code class = "language-Go" > dilbert.Salary -= 5000 // demoted, for writing too few lines of code
< / code > < / pre >
< p > 或者是对成员取地址,然后通过指针访问:< / p >
< pre > < code class = "language-Go" > position := & dilbert.Position
*position = " Senior " + *position // promoted, for outsourcing to Elbonia
< / code > < / pre >
< p > 点操作符也可以和指向结构体的指针一起工作:< / p >
< pre > < code class = "language-Go" > var employeeOfTheMonth *Employee = & dilbert
employeeOfTheMonth.Position += " (proactive team player)"
< / code > < / pre >
< p > 相当于下面语句< / p >
< pre > < code class = "language-Go" > (*employeeOfTheMonth).Position += " (proactive team player)"
< / code > < / pre >
< p > 下面的EmployeeByID函数将根据给定的员工ID返回对应的员工信息结构体的指针。我们可以使用点操作符来访问它里面的成员: < / p >
< pre > < code class = "language-Go" > func EmployeeByID(id int) *Employee { /* ... */ }
fmt.Println(EmployeeByID(dilbert.ManagerID).Position) // " Pointy-haired boss"
id := dilbert.ID
EmployeeByID(id).Salary = 0 // fired for... no real reason
< / code > < / pre >
< p > 后面的语句通过EmployeeByID返回的结构体指针更新了Employee结构体的成员。如果将EmployeeByID函数的返回值从< code > *Employee< / code > 指针类型改为Employee值类型, 那么更新语句将不能编译通过, 因为在赋值语句的左边并不确定是一个变量( 译注: 调用函数返回的是值, 并不是一个可取地址的变量) 。< / p >
< p > 通常一行对应一个结构体成员, 成员的名字在前类型在后, 不过如果相邻的成员类型如果相同的话可以被合并到一行, 就像下面的Name和Address成员那样: < / p >
< pre > < code class = "language-Go" > type Employee struct {
ID int
Name, Address string
DoB time.Time
Position string
Salary int
ManagerID int
}
< / code > < / pre >
< p > 结构体成员的输入顺序也有重要的意义。我们也可以将Position成员合并( 因为也是字符串类型) , 或者是交换Name和Address出现的先后顺序, 那样的话就是定义了不同的结构体类型。通常, 我们只是将相关的成员写到一起。< / p >
< p > 如果结构体成员名字是以大写字母开头的, 那么该成员就是导出的; 这是Go语言导出规则决定的。一个结构体可能同时包含导出和未导出的成员。< / p >
< p > 结构体类型往往是冗长的, 因为它的每个成员可能都会占一行。虽然我们每次都可以重写整个结构体成员, 但是重复会令人厌烦。因此, 完整的结构体写法通常只在类型声明语句的地方出现, 就像Employee类型声明语句那样。< / p >
< p > 一个命名为S的结构体类型将不能再包含S类型的成员: 因为一个聚合的值不能包含它自身。( 该限制同样适用于数组。) 但是S类型的结构体可以包含< code > *S< / code > 指针类型的成员,这可以让我们创建递归的数据结构,比如链表和树结构等。在下面的代码中,我们使用一个二叉树来实现一个插入排序:< / p >
< p > < u > < i > gopl.io/ch4/treesort< / i > < / u > < / p >
< pre > < code class = "language-Go" > type tree struct {
value int
left, right *tree
}
// Sort sorts values in place.
func Sort(values []int) {
var root *tree
for _, v := range values {
root = add(root, v)
}
appendValues(values[:0], root)
}
// appendValues appends the elements of t to values in order
// and returns the resulting slice.
func appendValues(values []int, t *tree) []int {
if t != nil {
values = appendValues(values, t.left)
values = append(values, t.value)
values = appendValues(values, t.right)
}
return values
}
func add(t *tree, value int) *tree {
if t == nil {
// Equivalent to return & tree{value: value}.
t = new(tree)
t.value = value
return t
}
if value < t.value {
t.left = add(t.left, value)
} else {
t.right = add(t.right, value)
}
return t
}
< / code > < / pre >
< p > 结构体类型的零值是每个成员都是零值。通常会将零值作为最合理的默认值。例如, 对于bytes.Buffer类型, 结构体初始值就是一个随时可用的空缓存, 还有在第9章将会讲到的sync.Mutex的零值也是有效的未锁定状态。有时候这种零值可用的特性是自然获得的, 但是也有些类型需要一些额外的工作。< / p >
< p > 如果结构体没有任何成员的话就是空结构体, 写作struct{}。它的大小为0, 也不包含任何信息, 但是有时候依然是有价值的。有些Go语言程序员用map来模拟set数据结构时, 用它来代替map中布尔类型的value, 只是强调key的重要性, 但是因为节约的空间有限, 而且语法比较复杂, 所以我们通常会避免这样的用法。< / p >
< pre > < code class = "language-Go" > seen := make(map[string]struct{}) // set of strings
// ...
if _, ok := seen[s]; !ok {
seen[s] = struct{}{}
// ...first time seeing s...
}
< / code > < / pre >
< h3 id = "441-结构体字面值" > < a class = "header" href = "#441-结构体字面值" > 4.4.1. 结构体字面值< / a > < / h3 >
< p > 结构体值也可以用结构体字面值表示,结构体字面值可以指定每个成员的值。< / p >
< pre > < code class = "language-Go" > type Point struct{ X, Y int }
p := Point{1, 2}
< / code > < / pre >
< p > 这里有两种形式的结构体字面值语法, 上面的是第一种写法, 要求以结构体成员定义的顺序为每个结构体成员指定一个字面值。它要求写代码和读代码的人要记住结构体的每个成员的类型和顺序, 不过结构体成员有细微的调整就可能导致上述代码不能编译。因此, 上述的语法一般只在定义结构体的包内部使用, 或者是在较小的结构体中使用, 这些结构体的成员排列比较规则, 比如image.Point{x, y}或color.RGBA{red, green, blue, alpha}。< / p >
< p > 其实更常用的是第二种写法, 以成员名字和相应的值来初始化, 可以包含部分或全部的成员, 如1.4节的Lissajous程序的写法: < / p >
< pre > < code class = "language-Go" > anim := gif.GIF{LoopCount: nframes}
< / code > < / pre >
< p > 在这种形式的结构体字面值写法中,如果成员被忽略的话将默认用零值。因为提供了成员的名字,所以成员出现的顺序并不重要。< / p >
< p > 两种不同形式的写法不能混合使用。而且,你不能企图在外部包中用第一种顺序赋值的技巧来偷偷地初始化结构体中未导出的成员。< / p >
< pre > < code class = "language-Go" > package p
type T struct{ a, b int } // a and b are not exported
package q
import " p"
var _ = p.T{a: 1, b: 2} // compile error: can't reference a, b
var _ = p.T{1, 2} // compile error: can't reference a, b
< / code > < / pre >
< p > 虽然上面最后一行代码的编译错误信息中并没有显式提到未导出的成员,但是这样企图隐式使用未导出成员的行为也是不允许的。< / p >
< p > 结构体可以作为函数的参数和返回值。例如, 这个Scale函数将Point类型的值缩放后返回: < / p >
< pre > < code class = "language-Go" > func Scale(p Point, factor int) Point {
return Point{p.X * factor, p.Y * factor}
}
fmt.Println(Scale(Point{1, 2}, 5)) // " {5 10}"
< / code > < / pre >
< p > 如果考虑效率的话,较大的结构体通常会用指针的方式传入和返回,< / p >
< pre > < code class = "language-Go" > func Bonus(e *Employee, percent int) int {
return e.Salary * percent / 100
}
< / code > < / pre >
< p > 如果要在函数内部修改结构体成员的话, 用指针传入是必须的; 因为在Go语言中, 所有的函数参数都是值拷贝传入的, 函数参数将不再是函数调用时的原始变量。< / p >
< pre > < code class = "language-Go" > func AwardAnnualRaise(e *Employee) {
e.Salary = e.Salary * 105 / 100
}
< / code > < / pre >
< p > 因为结构体通常通过指针处理,可以用下面的写法来创建并初始化一个结构体变量,并返回结构体的地址:< / p >
< pre > < code class = "language-Go" > pp := & Point{1, 2}
< / code > < / pre >
< p > 它和下面的语句是等价的< / p >
< pre > < code class = "language-Go" > pp := new(Point)
*pp = Point{1, 2}
< / code > < / pre >
< p > 不过& Point{1, 2}写法可以直接在表达式中使用,比如一个函数调用。< / p >
< h3 id = "442-结构体比较" > < a class = "header" href = "#442-结构体比较" > 4.4.2. 结构体比较< / a > < / h3 >
< p > 如果结构体的全部成员都是可以比较的,那么结构体也是可以比较的,那样的话两个结构体将可以使用==或!=运算符进行比较。相等比较运算符==将比较两个结构体的每个成员,因此下面两个比较的表达式是等价的:< / p >
< pre > < code class = "language-Go" > type Point struct{ X, Y int }
p := Point{1, 2}
q := Point{2, 1}
fmt.Println(p.X == q.X & & p.Y == q.Y) // " false"
fmt.Println(p == q) // " false"
< / code > < / pre >
< p > 可比较的结构体类型和其他可比较的类型一样, 可以用于map的key类型。< / p >
< pre > < code class = "language-Go" > type address struct {
hostname string
port int
}
hits := make(map[address]int)
hits[address{" golang.org" , 443}]++
< / code > < / pre >
< h3 id = "443-结构体嵌入和匿名成员" > < a class = "header" href = "#443-结构体嵌入和匿名成员" > 4.4.3. 结构体嵌入和匿名成员< / a > < / h3 >
< p > 在本节中, 我们将看到如何使用Go语言提供的不同寻常的结构体嵌入机制让一个命名的结构体包含另一个结构体类型的匿名成员, 这样就可以通过简单的点运算符x.f来访问匿名成员链中嵌套的x.d.e.f成员。< / p >
< p > 考虑一个二维的绘图程序,提供了一个各种图形的库,例如矩形、椭圆形、星形和轮形等几何形状。这里是其中两个的定义:< / p >
< pre > < code class = "language-Go" > type Circle struct {
X, Y, Radius int
}
type Wheel struct {
X, Y, Radius, Spokes int
}
< / code > < / pre >
< p > 一个Circle代表的圆形类型包含了标准圆心的X和Y坐标信息, 和一个Radius表示的半径信息。一个Wheel轮形除了包含Circle类型所有的全部成员外, 还增加了Spokes表示径向辐条的数量。我们可以这样创建一个wheel变量: < / p >
< pre > < code class = "language-Go" > var w Wheel
w.X = 8
w.Y = 8
w.Radius = 5
w.Spokes = 20
< / code > < / pre >
< p > 随着库中几何形状数量的增多,我们一定会注意到它们之间的相似和重复之处,所以我们可能为了便于维护而将相同的属性独立出来:< / p >
< pre > < code class = "language-Go" > type Point struct {
X, Y int
}
type Circle struct {
Center Point
Radius int
}
type Wheel struct {
Circle Circle
Spokes int
}
< / code > < / pre >
< p > 这样改动之后结构体类型变的清晰了,但是这种修改同时也导致了访问每个成员变得繁琐:< / p >
< pre > < code class = "language-Go" > var w Wheel
w.Circle.Center.X = 8
w.Circle.Center.Y = 8
w.Circle.Radius = 5
w.Spokes = 20
< / code > < / pre >
< p > Go语言有一个特性让我们只声明一个成员对应的数据类型而不指名成员的名字; 这类成员就叫匿名成员。匿名成员的数据类型必须是命名的类型或指向一个命名的类型的指针。下面的代码中, Circle和Wheel各自都有一个匿名成员。我们可以说Point类型被嵌入到了Circle结构体, 同时Circle类型被嵌入到了Wheel结构体。< / p >
< pre > < code class = "language-Go" > type Circle struct {
Point
Radius int
}
type Wheel struct {
Circle
Spokes int
}
< / code > < / pre >
< p > 得益于匿名嵌入的特性,我们可以直接访问叶子属性而不需要给出完整的路径:< / p >
< pre > < code class = "language-Go" > var w Wheel
w.X = 8 // equivalent to w.Circle.Point.X = 8
w.Y = 8 // equivalent to w.Circle.Point.Y = 8
w.Radius = 5 // equivalent to w.Circle.Radius = 5
w.Spokes = 20
< / code > < / pre >
< p > 在右边的注释中给出的显式形式访问这些叶子成员的语法依然有效, 因此匿名成员并不是真的无法访问了。其中匿名成员Circle和Point都有自己的名字——就是命名的类型名字——但是这些名字在点操作符中是可选的。我们在访问子成员的时候可以忽略任何匿名成员部分。< / p >
< p > 不幸的是,结构体字面值并没有简短表示匿名成员的语法, 因此下面的语句都不能编译通过:< / p >
< pre > < code class = "language-Go" > w = Wheel{8, 8, 5, 20} // compile error: unknown fields
w = Wheel{X: 8, Y: 8, Radius: 5, Spokes: 20} // compile error: unknown fields
< / code > < / pre >
< p > 结构体字面值必须遵循形状类型声明时的结构,所以我们只能用下面的两种语法,它们彼此是等价的:< / p >
< p > < u > < i > gopl.io/ch4/embed< / i > < / u > < / p >
< pre > < code class = "language-Go" > w = Wheel{Circle{Point{8, 8}, 5}, 20}
w = Wheel{
Circle: Circle{
Point: Point{X: 8, Y: 8},
Radius: 5,
},
Spokes: 20, // NOTE: trailing comma necessary here (and at Radius)
}
fmt.Printf(" %#v\n" , w)
// Output:
// Wheel{Circle:Circle{Point:Point{X:8, Y:8}, Radius:5}, Spokes:20}
w.X = 42
fmt.Printf(" %#v\n" , w)
// Output:
// Wheel{Circle:Circle{Point:Point{X:42, Y:8}, Radius:5}, Spokes:20}
< / code > < / pre >
< p > 需要注意的是Printf函数中%v参数包含的#副词, 它表示用和Go语言类似的语法打印值。对于结构体类型来说, 将包含每个成员的名字。< / p >
< p > 因为匿名成员也有一个隐式的名字, 因此不能同时包含两个类型相同的匿名成员, 这会导致名字冲突。同时, 因为成员的名字是由其类型隐式地决定的, 所以匿名成员也有可见性的规则约束。在上面的例子中, Point和Circle匿名成员都是导出的。即使它们不导出( 比如改成小写字母开头的point和circle) , 我们依然可以用简短形式访问匿名成员嵌套的成员< / p >
< pre > < code class = "language-Go" > w.X = 8 // equivalent to w.circle.point.X = 8
< / code > < / pre >
< p > 但是在包外部, 因为circle和point没有导出, 不能访问它们的成员, 因此简短的匿名成员访问语法也是禁止的。< / p >
< p > 到目前为止,我们看到匿名成员特性只是对访问嵌套成员的点运算符提供了简短的语法糖。稍后,我们将会看到匿名成员并不要求是结构体类型;其实任何命名的类型都可以作为结构体的匿名成员。但是为什么要嵌入一个没有任何子成员类型的匿名成员类型呢?< / p >
< p > 答案是匿名类型的方法集。简短的点运算符语法可以用于选择匿名成员嵌套的成员, 也可以用于访问它们的方法。实际上, 外层的结构体不仅仅是获得了匿名成员类型的所有成员, 而且也获得了该类型导出的全部的方法。这个机制可以用于将一些有简单行为的对象组合成有复杂行为的对象。组合是Go语言中面向对象编程的核心, 我们将在6.3节中专门讨论。< / p >
<!-- 公众号 -->
< hr >
< table >
< tr >
< td >
< img width = "222px" src = "https://chai2010.cn/advanced-go-programming-book/css.png" >
< / td >
< td >
< img width = "222px" src = "https://chai2010.cn/advanced-go-programming-book/cch.png" >
< / td >
< / tr >
< / table >
< div id = "giscus-container" > < / div >
< footer class = "page-footer" >
< span > © 2015-2016 | < a href = "https://github.com/gopl-zh" > Go语言圣经中文版< / a > , 仅学习交流使用< / span >
< / footer >
< / main >
< nav class = "nav-wrapper" aria-label = "Page navigation" >
<!-- Mobile navigation buttons -->
< a rel = "prev" href = "../ch4/ch4-03.html" class = "mobile-nav-chapters previous" title = "Previous chapter" aria-label = "Previous chapter" aria-keyshortcuts = "Left" >
< i class = "fa fa-angle-left" > < / i >
< / a >
< a rel = "next" href = "../ch4/ch4-05.html" class = "mobile-nav-chapters next" title = "Next chapter" aria-label = "Next chapter" aria-keyshortcuts = "Right" >
< i class = "fa fa-angle-right" > < / i >
< / a >
< div style = "clear: both" > < / div >
< / nav >
< / div >
< / div >
< nav class = "nav-wide-wrapper" aria-label = "Page navigation" >
< a rel = "prev" href = "../ch4/ch4-03.html" class = "nav-chapters previous" title = "Previous chapter" aria-label = "Previous chapter" aria-keyshortcuts = "Left" >
< i class = "fa fa-angle-left" > < / i >
< / a >
< a rel = "next" href = "../ch4/ch4-05.html" class = "nav-chapters next" title = "Next chapter" aria-label = "Next chapter" aria-keyshortcuts = "Right" >
< i class = "fa fa-angle-right" > < / i >
< / a >
< / nav >
< / div >
< script type = "text/javascript" >
window.playground_copyable = true;
< / script >
< script src = "../elasticlunr.min.js" type = "text/javascript" charset = "utf-8" > < / script >
< script src = "../mark.min.js" type = "text/javascript" charset = "utf-8" > < / script >
< script src = "../searcher.js" type = "text/javascript" charset = "utf-8" > < / script >
< script src = "../clipboard.min.js" type = "text/javascript" charset = "utf-8" > < / script >
< script src = "../highlight.js" type = "text/javascript" charset = "utf-8" > < / script >
< script src = "../book.js" type = "text/javascript" charset = "utf-8" > < / script >
< script type = "text/javascript" charset = "utf-8" >
var pagePath = "ch4/ch4-04.md"
< / script >
<!-- Custom JS scripts -->
< script type = "text/javascript" src = "../js/custom.js" > < / script >
< script type = "text/javascript" src = "../js/bigPicture.js" > < / script >
< / body >
< / html >