当前位置:网站首页>学习 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
边栏推荐
- Go interface usage
- Software testers, how to mention bugs?
- Ueditor -- limitation of 4m size of image upload component
- Common parameters of ffmpeg command line
- Is the pointer symbol of C language close to variable type or variable name?
- About the three commonly used auxiliary classes of JUC
- Hikvision face to face summary
- ConstraintLayout布局
- Microsoft Access database using PHP PDO ODBC sample
- 最强日期正则表达式
猜你喜欢

Go interface usage

Diary of dishes | Blue Bridge Cup - hexadecimal to octal (hand torn version) with hexadecimal conversion notes

Manjaro installation and configuration (vscode, wechat, beautification, input method)

Source insight 4.0 FAQs

【leetcode】107. Sequence traversal of binary tree II

Chapter 120 SQL function round

Excel·VBA自定义函数获取单元格多数值

How does the swagger2 interface import postman

Excel·VBA数组冒泡排序函数

Visual common drawing (III) area map
随机推荐
Difference between pregnancy box and delivery box
Software testers, how to mention bugs?
CUMCM 2021-B:乙醇偶合制備C4烯烴(2)
Visual common drawing (V) scatter diagram
ConstraintLayout布局
Cve-2019-0708 vulnerability exploitation of secondary vocational network security 2022 national competition
RESTful和SOAP的区别
SQLServer 查询数据库死锁
Xshell+Xftp 下载安装步骤
MBA-day6 逻辑学-假言推理练习题
一道有趣的阿里面试题
Restful、SOAP、RPC、SOA、微服务之间的区别
Let the LAN group use the remote device
Gets the current time in character format
全栈交叉编译X86完成过程经验分享
Reading integrity monitoring techniques for vision navigation systems - 5 Results
Constraintlayout layout
Excel·VBA自定义函数获取单元格多数值
Wonderful review | deepnova x iceberg meetup online "building a real-time data Lake based on iceberg"
26. 删除有序数组中的重复项