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条)

报告相同问题?

悬赏问题

  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?
  • ¥15 Ubuntu20.04.4.LTS系统如何下载安装VirtualBox虚拟机?
  • ¥15 如何用QDomDocument读取内容为空格的xml数据
  • ¥15 请阅读下面代码,帮我修改下代码