douxia2053 2015-10-29 15:17
浏览 803
已采纳

为什么我不能做fmt.Sprintf(“%d。%d。%d。%d”,a ...)?

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.

  • 写回答

6条回答 默认 最新

  • duanlu1959 2015-10-29 15:23
    关注

    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, 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, a)
        }
    }
    

    Output:

    loopback: 127.0.0.1
    googleDNS: 8.8.8.8
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向