mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-12 05:33:04 +08:00
+ ch10.3完成
This commit is contained in:
40
eBook/10.3.md
Normal file
40
eBook/10.3.md
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
# 10.3 使用自定义包中的结构体
|
||||||
|
|
||||||
|
下面的例子中,main.go使用了一个结构体,它来自**submap?** struct_pack下的包structPack。
|
||||||
|
|
||||||
|
Listing 10.5—structPack.go:
|
||||||
|
```go
|
||||||
|
package structPack
|
||||||
|
|
||||||
|
type ExpStruct struct {
|
||||||
|
Mi1 int
|
||||||
|
Mf1 float32
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Listing 10.6—main.go:
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"./struct_pack/structPack"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
struct1 := new(structPack.ExpStruct)
|
||||||
|
struct1.Mi1 = 10
|
||||||
|
struct1.Mf1 = 16.
|
||||||
|
|
||||||
|
fmt.Printf("Mi1 = %d\n", struct1.Mi1)
|
||||||
|
fmt.Printf("Mf1 = %f\n", struct1.Mf1)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
输出:
|
||||||
|
Mi1 = 10
|
||||||
|
Mf1 = 16.000000
|
||||||
|
|
||||||
|
## 链接
|
||||||
|
- [目录](directory.md)
|
||||||
|
- 上一节:[10.2 使用工厂方法创建结构体实例](10.2.md)
|
||||||
|
- 下一节:[10.4 Structs with tags](10.4.md)
|
@@ -87,6 +87,7 @@
|
|||||||
- 第10章:[结构(struct)与方法(method)](10.0.md)
|
- 第10章:[结构(struct)与方法(method)](10.0.md)
|
||||||
- 10.1 [结构体定义](10.1.md)
|
- 10.1 [结构体定义](10.1.md)
|
||||||
- 10.2 [使用工厂方法创建结构体实例](10.2.md)
|
- 10.2 [使用工厂方法创建结构体实例](10.2.md)
|
||||||
|
- 10.3 [使用自定义包中的结构体](10.3.md)
|
||||||
- 第11章:接口(interface)与反射(reflection)
|
- 第11章:接口(interface)与反射(reflection)
|
||||||
|
|
||||||
## 第三部分:Go 高级编程
|
## 第三部分:Go 高级编程
|
||||||
|
Reference in New Issue
Block a user