当前位置:网站首页>学习 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
边栏推荐
- JVM - common parameters
- Visual common drawing (III) area map
- Notes on concurrent programming of vegetables (IX) asynchronous IO to realize concurrent crawler acceleration
- Introduction to data analysis 𞓜 kaggle Titanic mission (IV) - > data cleaning and feature processing
- Restful、SOAP、RPC、SOA、微服务之间的区别
- MySQL Router重装后重新连接集群进行引导出现的——此主机中之前已配置过的问题
- Wonderful review | deepnova x iceberg meetup online "building a real-time data Lake based on iceberg"
- 《Neo4j权威指南》简介,求伯君、周鸿袆、胡晓峰、周涛等大咖隆重推荐
- ID number verification system based on visual structure - Raspberry implementation
- SWAT—Samba WEB管理工具介绍
猜你喜欢

【leetcode】107. Sequence traversal of binary tree II

Visual common drawing (III) area map

Jupyter Lab 十大高生产力插件

26. 删除有序数组中的重复项

Mysql8.0安装指南

高价买来的课程,公开了!phper资料分享

Solution architect's small bag - 5 types of architecture diagrams

Solutions to common problems in visualization (VIII) solutions to problems in shared drawing area

语雀文档编辑器将开源:始于但不止于Markdown

Reading integrity monitoring techniques for vision navigation systems - 5 Results
随机推荐
我的创作纪念日
Facing the global market, platefarm today logs in to four major global platforms such as Huobi
Download and installation steps of xshell + xftp
Visual common drawing (V) scatter diagram
SVN的使用:
Visualized common drawing (II) line chart
Precautions for latex formula
VScode
【leetcode】107.二叉树的层序遍历II
Installing MySQL with CentOS / Linux
Mba-day5 Mathematics - application problems - engineering problems
语雀文档编辑器将开源:始于但不止于Markdown
Cve-2019-0708 vulnerability exploitation of secondary vocational network security 2022 national competition
Full stack cross compilation x86 completion process experience sharing
Esp32 learning - use and configuration of GPIO
Swagger2 接口如何导入Postman
Mba-day6 logic - hypothetical reasoning exercises
Learning note 5 - gradient explosion and gradient disappearance (k-fold cross verification)
Is the pointer symbol of C language close to variable type or variable name?
Learning Notes 6 - Summary of several deep learning convolutional neural networks