我正在官方的“A Tour of Go”页面上执行一些任务。我已经定义了一个自定义类型,该类型为.IPAddrbyte[4]
假设类型的值为 。IPAddr{127, 2, 0, 1}
我需要重写该方法,以便它以形式而不是的形式打印。String()127.2.0.1[127, 2, 0, 1]
这是代码和我卡住的地方:
package main
import "fmt"
type IPAddr [4]byte
func (p IPAddr) String() string {
return string(p[0]) + "." + string(p[1]) + "." + string(p[2]) + "." + string(p[3]) // this here does not work.
//Even if I simply return p[0] nothing is returned back.
}
func main() {
a := IPAddr{127, 2, 54, 32}
fmt.Println("a:", a)
}
心有法竹
相关分类