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

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格