英文:
How To Convert Bytes Into Strings In Go[lang]
问题
我是你的中文翻译助手,以下是你要翻译的内容:
我是Go语言的新手,正在尝试完成Stringers练习,但是我无法在Go中将bytes
转换为string
。我查找了一些解决方案,比如string(i[:])
,但是它并不起作用。下面是我的完整代码:
package main
import (
"fmt"
)
type IPAddr [4]byte
func (i IPAddr) String() string {
// 不确定如何将字节转换为字符串?
// 期望的结果是:从 {127, 0, 0, 1} 转换为 127.0.0.1
return string(i[:])
}
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
希望能得到帮助。
谢谢,
DD.
英文:
I am new in Go and trying to do Stringers exercise but I am unable to convert bytes
to string
in Go. I looked and found a solution string(i[:])
but that is not working. Below is my complete code
package main
import (
"fmt"
)
type IPAddr [4]byte
func (i IPAddr) String() string {
// not sure how to turn bytes into string ?
// expected result: from {127, 0, 0, 1} -> 127.0.0.1
return string(i[:])
}
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)
}
}
expected result is
loopback: 127.0.0.1
googleDNS: 8.8.8.8
any help would be really appreciated.
CHeers,
DD.
答案1
得分: 2
将String()
更改为以下内容:
func (i IPAddr) String() string {
// return fmt.Sprintf("%d.%d.%d.%d", i[0], i[1], i[2], i[3])
var res string
res = strconv.Itoa(int(i[0]))
for _, v := range i[1:] {
res += "." + strconv.Itoa(int(v))
}
return res
// or
/*
var sb strings.Builder
sb.WriteString(strconv.Itoa(int(i[0])))
for _, v := range i[1:] {
sb.WriteString("." + strconv.Itoa(int(v)))
}
return sb.String()
*/
}
英文:
change String()
to this
func (i IPAddr) String() string {
// return fmt.Sprintf("%d.%d.%d.%d", i[0], i[1], i[2], i[3])
var res string
res = strconv.Itoa(int(i[0]))
for _, v := range i[1:] {
res += "." + strconv.Itoa(int(v))
}
return res
// or
/*
var sb strings.Builder
sb.WriteString(strconv.Itoa(int(i[0])))
for _, v := range i[1:] {
sb.WriteString("." + strconv.Itoa(int(v)))
}
return sb.String()
*/
}
答案2
得分: 2
将一个4字节数组转换为“点分十进制”(dotted quad)的“正确”方法是使用内置的net
包:
package main
import (
"fmt"
"net"
)
func main() {
octets := []byte{123, 45, 67, 89}
ip := net.IP(octets)
dottedQuad := ip.To4().String()
fmt.Printf("%v is %s\n", octets, dottedQuad)
}
英文:
The "right" way to convert a 4-byte array to a 'dotted quadwould be to use the in-built
net` package:
package main
import (
"fmt"
"net"
)
func main() {
octets := []byte{123, 45, 67, 89}
ip := net.IP(octets)
dottedQuad := ip.To4().String()
fmt.Printf("%v is %s\n", octets, dottedQuad)
}
答案3
得分: 1
你不能直接将UTF-8编码的值输出为字符串,127被视为UTF-8值而不是字符串,所以你应该先将整数转换为字符串。在Golang中,整数类型不能直接转换为字符串,需要使用函数如strconv.Itoa()
或fmt.Sprintf('%d', int)
。
你的代码可以像这样写:
func (i IPAddr) String() string {
return fmt.Sprintf("%v.%v.%v.%v", i[0], i[1], i[2], i[3])
}
英文:
You can't just output the UTF-8 encoded value as the string
the 127 take as the UTF-8 value not the string
so you should change the integer to the string first.
And in Golang integer type can not directly transform to string without function like strconv.Itoa()
or fmt.Sprintf('%d', int)
your code can be like
func (i IPAddr) String() string {
return return fmt.Sprintf("%v.%v.%v.%v", i[0], i[1], i[2], i[3])
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论