当前位置:网站首页>学习 Go 语言 0x07:《Go 语言之旅》中 Stringer 练习题代码
学习 Go 语言 0x07:《Go 语言之旅》中 Stringer 练习题代码
2022-04-23 11:02:00 【爱博客大伯】
* 题目
https://tour.go-zh.org/methods/18
练习:Stringer
通过让 IPAddr 类型实现 fmt.Stringer 来打印点号分隔的地址。
例如,IPAddr{1, 2, 3, 4} 应当打印为 “1.2.3.4”。
* 实现方式一:使用strconv
strconv
的使用,参考:https://golang.google.cn/pkg/strconv/
strconv.Itoa(int(val))
是将 int 整数转换为字符串。
由于 IPAddr 是4个 byte,所以需要用 int(val)
转换成 int 类型。
package main
import "fmt"
import "strconv"
type IPAddr [4]byte
// TODO: 给 IPAddr 添加一个 "String() string" 方法
func (v IPAddr) String() string {
var s string
for idx, val := range v {
if idx != 3 {
s += strconv.Itoa(int(val)) + "."
} else {
s += strconv.Itoa(int(val))
}
}
return s
}
func main() {
hosts := map[string]IPAddr{
"loopback": {
127, 0, 0, 1},
"googleDNS": {
8, 8, 8, 8},
}
for name, ip := range hosts {
fmt.Printf("%v: %v\n", name, ip)
}
}
输出:
loopback: 127.0.0.1
googleDNS: 8.8.8.8
* 实现方式二:使用fmt.Sprintf
package main
import "fmt"
type IPAddr [4]byte
// TODO: 给 IPAddr 添加一个 "String() string" 方法
func (v IPAddr) String() string {
return fmt.Sprintf("%v.%v.%v.%v", v[0], v[1], v[2], v[3])
}
func main() {
hosts := map[string]IPAddr{
"loopback": {
127, 0, 0, 1},
"googleDNS": {
8, 8, 8, 8},
}
for name, ip := range hosts {
fmt.Printf("%v: %v\n", name, ip)
}
}
版权声明
本文为[爱博客大伯]所创,转载请带上原文链接,感谢
https://blog.csdn.net/u013553529/article/details/88919386
边栏推荐
- Google Earth Engine(GEE)——将原始影像进行升尺度计算(以海南市为例)
- Diary of dishes | Blue Bridge Cup - hexadecimal to octal (hand torn version) with hexadecimal conversion notes
- 图像处理——噪声小记
- Swagger2 自定义参数注解如何不显示
- VIM usage
- 最强日期正则表达式
- Cumcm 2021 - B: préparation d'oléfines C4 par couplage éthanol (2)
- colab
- 期货开户哪个公司好?安全靠谱的期货公司谁能推荐几家?
- Understand the key points of complement
猜你喜欢
Jupyter Lab 十大高生产力插件
Introduction to wechat applet, development history, advantages of applet, application account, development tools, initial knowledge of wxml file and wxss file
ConstraintLayout布局
SQL Server cursor circular table data
Diary of dishes | Blue Bridge Cup - hexadecimal to octal (hand torn version) with hexadecimal conversion notes
More reliable model art than deep learning
Visual common drawing (I) stacking diagram
How does the swagger2 interface import postman
Introduction to data analysis 𞓜 kaggle Titanic mission (III) - > explore data analysis
Notes on concurrent programming of vegetables (IX) asynchronous IO to realize concurrent crawler acceleration
随机推荐
Typora operation skill description (I) md
Chapter 120 SQL function round
Alarm scene recognition
Introduction to wechat applet, development history, advantages of applet, application account, development tools, initial knowledge of wxml file and wxss file
全栈交叉编译X86完成过程经验分享
学习网站资料
Notes on concurrent programming of vegetables (V) thread safety and lock solution
Ueditor -- limitation of 4m size of image upload component
语雀文档编辑器将开源:始于但不止于Markdown
Latex usage
After the MySQL router is reinstalled, it reconnects to the cluster for boot - a problem that has been configured in this host before
STM32接电机驱动,杜邦线供电,然后反烧问题
SVN的使用:
Data analysis learning (I) data analysis and numpy Foundation
SQL Server recursive query of superior and subordinate
Derivation and regularization
CUMCM 2021-B:乙醇偶合制备C4烯烃(2)
Common parameters of ffmpeg command line
26. 删除有序数组中的重复项
Esp32 learning - use and configuration of GPIO