英文:
Why can't I do fmt.Sprintf("%d.%d.%d.%d", a...)?
问题
我正在学习Go,并且在Go tour(exercise-stringer.go: https://tour.golang.org/methods/7)中遇到了困难。
以下是一些代码:
type IPAddr [4]byte
// TODO: 为IPAddr添加一个 "String() string" 方法。
func (a IPAddr) String() string {
return fmt.Sprintf("%d.%d.%d.%d", a...)
}
所以我认为IPAddr
的内部表示是[4]byte
,所以扩展运算符应该可以工作。但是我得到了以下错误:
cannot use []string literal (type []string) as type []interface {} in argument to fmt.Sprintf
这是怎么回事?字符串切片也不起作用,这里发生了什么?
编辑:对不起,我的问题中有一个错误 - 错误是关于类型IPAddr
,而不是[]string
。我在尝试代码时粘贴了错误的输出。无论如何,感谢peterSO和0x434D53关于Go中切片的不变性的解释。
好吧,这引发了另一个问题。为什么要以这种方式实现呢?我想象中你只需要一个Iterable
接口,那么任何实现它的结构体都会“正常工作”。
附注:当我第一次听说Go时,有这样一个大胆的说法:“编译,但富有表现力”。显式接口实现就是这一说法的很好例子,但是显式转换、缺乏运算符重载等等给我一种“90年代的Java感觉”。这很令人遗憾,因为Go看起来是一门很棒的语言。
英文:
I'm learning Go and I'm stuck with Go tour (exercise-stringer.go: https://tour.golang.org/methods/7).
Here's some code:
type IPAddr [4]byte
// TODO: Add a "String() string" method to IPAddr.
func (a IPAddr) String() string {
return fmt.Sprintf("%d.%d.%d.%d", a...)
}
So I figured the inner representation of IPAddr
is [4]byte
, so spread operator works. But I'm getting:
cannot use []string literal (type []string) as type []interface {} in argument to fmt.Sprintf
What the heck? String slice doesn't work either, what's going on here?
EDIT: Sorry, there's an error in my question - error was about type IPAddr
, not []string
. I was playing with the code and I've pasted wrong output. Anyway, thanks to peterSO and 0x434D53 about invariance of slices in Go.
Well, this raises another question. Why is it implemented in this way? I imagine you'd just have some Iterable
interface, so any struct implementing it would "just work".
Sidenote: when I first heard about Go there was this bold statement "compiled, but expressive". And explicit interface implementation is great example of this, but things like explicit conversion, lack of operator overloading and so on give me "90s Java feel". Which is sad, because Go seems like a great language.
1: https://stackoverflow.com/users/221700/peterso "peterSO"
2: https://stackoverflow.com/users/437276/0x434d53 "0x434D53"
答案1
得分: 13
【A Tour of Go】
练习:Stringers
使IPAddr
类型实现fmt.Stringer
接口,以便将地址打印为点分十进制。
例如,IPAddr{1, 2, 3, 4}
应该打印为"1.2.3.4"
。
package main
import "fmt"
type IPAddr [4]byte
// TODO: 为IPAddr添加一个"String() string"方法。
func main() {
addrs := map[string]IPAddr{
"loopback": {127, 0, 0, 1},
"googleDNS": {8, 8, 8, 8},
}
for n, a := range addrs {
fmt.Printf("%v: %v\n", n, a)
}
}
[]string
不能隐式转换为[]interface{}
。请参阅The Go Programming Language Specification中的Conversions。您需要提供显式转换。例如,
package main
import "fmt"
type IPAddr [4]byte
// IPAddr的"String() string"方法。
func (a IPAddr) String() string {
return fmt.Sprintf("%d.%d.%d.%d", a[0], a[1], a[2], a[3])
}
func main() {
addrs := map[string]IPAddr{
"loopback": {127, 0, 0, 1},
"googleDNS": {8, 8, 8, 8},
}
for n, a := range addrs {
fmt.Printf("%v: %v\n", n, a)
}
}
输出:
loopback: 127.0.0.1
googleDNS: 8.8.8.8
英文:
> A Tour of Go
>
> Exercise: Stringers
>
> Make the IPAddr
type implement fmt.Stringer
to print the address as a
> dotted quad.
>
> For instance, IPAddr{1, 2, 3, 4}
should print as "1.2.3.4
".
>
> package main
>
> import "fmt"
>
> type IPAddr [4]byte
>
> // TODO: Add a "String() string" method to IPAddr.
>
> func main() {
> addrs := map[string]IPAddr{
> "loopback": {127, 0, 0, 1},
> "googleDNS": {8, 8, 8, 8},
> }
> for n, a := range addrs {
> fmt.Printf("%v: %v\n", n, a)
> }
> }
There is no implicit conversion of []string
to []interface {}
. See Conversions in The Go Programming Language Specification. You need to provide an explicit conversion. For example,
package main
import "fmt"
type IPAddr [4]byte
// A "String() string" method for IPAddr.
func (a IPAddr) String() string {
return fmt.Sprintf("%d.%d.%d.%d", a[0], a[1], a[2], a[3])
}
func main() {
addrs := map[string]IPAddr{
"loopback": {127, 0, 0, 1},
"googleDNS": {8, 8, 8, 8},
}
for n, a := range addrs {
fmt.Printf("%v: %v\n", n, a)
}
}
Output:
loopback: 127.0.0.1
googleDNS: 8.8.8.8
答案2
得分: 10
根据Go FAQ中的说明,无法将一个有类型的数组隐式转换为[]interface{}
:
这是由于语言规范禁止这样做,因为这两种类型在内存中的表示方式不同。需要逐个复制元素到目标切片中。
下面的解决方案可以实现,但需要创建一个中间切片:
func (ip IPAddr) String() string {
tmp := make([]interface{}, len(ip))
for i, val := range ip {
tmp[i] = val
}
return fmt.Sprintf("%d.%d.%d.%d", tmp...)
}
英文:
As stated in the Go FAQ section Can I convert a []T to an []interface{}, there is no implicit conversion from a typed array to an []interface{}
:
> It is disallowed by the language specification because the two types
> do not have the same representation in memory. It is necessary to copy
> the elements individually to the destination slice
The following solution works but require the creation of an intermediate slice:
func (ip IPAddr) String() string {
tmp := make([]interface{}, len(ip))
for i, val := range ip {
tmp[i] = val
}
return fmt.Sprintf("%d.%d.%d.%d", tmp...)
}
答案3
得分: 4
根据Go语言规范:
如果函数f的最后一个参数p的类型是...T,那么在函数f内部,参数p的类型等同于[]T类型。
但是在Go语言中,切片(slices)和数组(arrays)是类型不变的。所以,如果T和U是不同的类型,那么[]T
和[]U
是不同的。它们没有任何关联,即使T是U的结构子类型。因此,[]string
不是[]interface
。
英文:
From the go language specification:
> If f is variadic with a final parameter p of type ...T, then within f the type of p is equivalent to type []T
But in Go slices and arrays are type invariant. So an []T
is different from []U
if T
and U
are different types. They are not related at all, even if T
is an structural subtype of U
. So []string
is not an []interface
.
答案4
得分: 0
你需要为Stringer接口实现这个方法。
func (ip IPAddr) String() string {
return fmt.Sprintf("%v.%v.%v.%v", ip[0], ip[1], ip[2], ip[3])
}
英文:
You need to implement this method for the Stringer interface.
func (ip IPAddr) String() string {
return fmt.Sprintf("%v.%v.%v.%v", ip[0], ip[1], ip[2], ip[3])
}
答案5
得分: 0
range
应该用于遍历数组,所以答案应该如下所示:
func (ip IPAddr) String() string {
out := fmt.Sprintf("%v", ip[0])
for _, value := range ip[1:] {
out += fmt.Sprintf(".%v", value)
}
return out
}
英文:
The range
should be used to iterate over the array, so the answer would be like as follows:
func (ip IPAddr) String() string {
out := fmt.Sprintf("%v", ip[0])
for _, value := range ip[1:] {
out += fmt.Sprintf(".%v", value)
}
return out
}
答案6
得分: 0
func (ip IPAddr) String() string {
var s []string;
for _, v := range ip {
s = append(s, fmt.Sprintf("%v", v))
}
return strings.Join(s, ".")
}
英文:
func (ip IPAddr) String() string {
var s []string;
for _, v := range ip {
s = append(s, fmt.Sprintf("%v", v))
}
return strings.Join(s, ".")
}
答案7
得分: -1
首先,当我运行以下代码时:
package main
import "fmt"
type IPAddr [4]byte
// TODO: Add a "String() string" method to IPAddr.
func (a IPAddr) String() string {
return fmt.Sprintf("%d.%d.%d.%d", a...)
}
func main() {
addrs := map[string]IPAddr{
"loopback": {127, 0, 0, 1},
"googleDNS": {8, 8, 8, 8},
}
for n, a := range addrs {
fmt.Printf("%v: %v\n", n, a)
}
}
错误信息为:
prog.go:9: cannot use a (type IPAddr) as type []interface {} in argument to fmt.Sprintf
而不是
cannot use []string literal (type []string) as type []interface {} in argument to fmt.Sprintf
因此,我认为在复制和粘贴过程中出现了一些问题。
type IPAddr [4]byte
并没有定义一个字符串,所以问题中的错误信息是误导性的。
它是一个 [4]byte
,从 Go 语言类型的角度来看,它是一个完全不同的类型。它也不是 []byte
。
type IPAddr [4]byte
也不满足一个接口,例如实现 String() 方法,这样 fmt.Sprintf 就可以使用它,因为 IPAddr 的 String() 方法不会被编译。
你可以尝试将 [4]byte
转换为字符串,但是这种转换 string(a)
是不合法的。更糟糕的是,这四个字节值将被视为字符代码,而不是转换为表示这四个小整数值的字符。很可能一些 IPAddr 的字节值可能是无效的 UTF-8,如果程序尝试打印它,那就更奇怪了。
如其他答案中所解释的那样,
return fmt.Sprintf("%d.%d.%d.%d", a[0], a[1], a[2], a[3])
以你期望的格式返回 IPAddr 的字符串值。
一旦 func (a IPAddr) String() string
是有效的,它就会起作用;IPAddr
实现了 fmt.Stringer 接口。
然后,在
fmt.Printf("%v: %v\n", n, a)
中的 %v
可以被替换为
fmt.Printf("%s: %s\n", n, a)
因为 fmt 输出方法已经实现了 String()。
我更喜欢 %s
而不是 %v
,因为它表示程序不依赖于一个“默认的 Go 值表示”(即对于数组 [127 0 0 1]
),并且该类型实现了 String()。
英文:
Firstly, when I 'run':
package main
import "fmt"
type IPAddr [4]byte
// TODO: Add a "String() string" method to IPAddr.
func (a IPAddr) String() string {
return fmt.Sprintf("%d.%d.%d.%d", a...)
}
func main() {
addrs := map[string]IPAddr{
"loopback": {127, 0, 0, 1},
"googleDNS": {8, 8, 8, 8},
}
for n, a := range addrs {
fmt.Printf("%v: %v\n", n, a)
}
}
The error is:
> prog.go:9: cannot use a (type IPAddr) as type []interface {} in
> argument to fmt.Sprintf
and not
> cannot use []string literal (type []string) as type []interface {} in
> argument to fmt.Sprintf
So, I think something got out of synch while copying and pasting.
type IPAddr [4]byte
does not define a string, so the error message in the question is misleading.
It's a [4]byte
, a completely different type (from a Go language type perspective) from string. It isn't a []byte
either.
Nor does type IPAddr [4]byte
satisfy an interface, for instance implement String(), that fmt.Sprintf could use, because IPAddr's String() method doesn't get compiled.
You might try to convert the [4]byte
to a string, but that conversion, string(a)
isn't legal. Worse, the four byte values would be treated as character codes, and not converted to a character representation of the 4 small integer values. It is quite likely that some IPAddr byte values might be invalid UTF-8 which would be even more weird if a program tried to print it.
As explained in other answers,
return fmt.Sprintf("%d.%d.%d.%d", a[0], a[1], a[2], a[3])
returns a string value of IPAddr in the format you are aiming for.
Once func (a IPAddr) String() string
is valid, it works; IPAddr
implements the fmt.Stringer interface.
Then %v
in
fmt.Printf("%v: %v\n", n, a)
can be replaced by %s
in
fmt.Printf("%s: %s\n", n, a)
because the fmt output methods have an implementation of String().
I prefer %s
to %v
because it signals that the program is not relying on a 'default Go value representation' (i.e. for that array [127 0 0 1]
), and that the type implements String().
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论