当前位置:网站首页>Learn go language 0x07: stringer exercise code in go language journey
Learn go language 0x07: stringer exercise code in go language journey
2022-04-23 11:08:00 【Love blog uncle】
* subject
https://tour.go-zh.org/methods/18
practice :Stringer
By making IPAddr Type implementation fmt.Stringer To print dot separated addresses .
for example ,IPAddr{1, 2, 3, 4} Should be printed as “1.2.3.4”.
* Implementation mode I : Use strconv
strconv Use , Reference resources :https://golang.google.cn/pkg/strconv/
strconv.Itoa(int(val)) Yes, it will int Integer to string .
because IPAddr yes 4 individual byte, So we need to use int(val) convert to int type .
package main
import "fmt"
import "strconv"
type IPAddr [4]byte
// TODO: to IPAddr Add one "String() string" Method
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)
}
}
Output :
loopback: 127.0.0.1
googleDNS: 8.8.8.8
* Implementation mode II : Use fmt.Sprintf
package main
import "fmt"
type IPAddr [4]byte
// TODO: to IPAddr Add one "String() string" Method
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)
}
}
版权声明
本文为[Love blog uncle]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231102105873.html
边栏推荐
- Esp32 learning - use and configuration of GPIO
- Intuitive understanding entropy
- MySQL interview questions explain how to set hash index
- MySQL sorting feature details
- Using El popconfirm and El backtop does not take effect
- Jupyter lab top ten high productivity plug-ins
- VScode
- Mba-day5 Mathematics - application problems - engineering problems
- 软件测试人员,如何优秀的提Bug?
- Let the LAN group use the remote device
猜你喜欢

The songbird document editor will be open source: starting with but not limited to markdown

CUMCM 2021-B:乙醇偶合制備C4烯烴(2)

CUMCM 2021-B:乙醇偶合制备C4烯烃(2)

Structure of C language (Advanced)

An interesting interview question

ConstraintLayout布局

Google Earth engine (GEE) - scale up the original image (taking Hainan as an example)

进程间通信 -- 消息队列

Visualized common drawing (II) line chart

Database management software sqlpro for SQLite for Mac 2022.30
随机推荐
Resolution and size of mainstream mobile phones
MySQL数据库10秒内插入百万条数据的实现
An interesting interview question
Software testers, how to mention bugs?
Go interface usage
Three web components (servlet, filter, listener)
解决 『SunCertPathBuilderException:unable to find valid certification path to requested target』 问题
期货开户哪个公司好?安全靠谱的期货公司谁能推荐几家?
MySQL8.0升级的踩坑历险记
ConstraintLayout布局
VM set up static virtual machine
mysql中整数数据类型tinyint详解
Precautions for latex formula
学习 Go 语言 0x08:《Go 语言之旅》中 练习使用 error
More reliable model art than deep learning
Simple thoughts on the design of a microblog database
Mysql8. 0 installation guide
Use of SVN:
Visual solutions to common problems (VIII) mathematical formulas
MySQL数据库事务transaction示例讲解教程