【问题标题】:Hidden methods in type implementations?类型实现中的隐藏方法?
【发布时间】:2019-10-31 22:43:48
【问题描述】:

浏览net/http 和相关库中的一些go 源,我遇到了something,这让我很好奇。我在这里查看的是 1.12 版。

func (p *ReverseProxy) handleUpgradeResponse(rw http.ResponseWriter, req *http.Request, res *http.Response) {
  ...
  hj, ok := rw.(http.Hijacker)
  ...
  conn, brw, err := hj.Hijack()
  ...
}

我已经在更多地方以及标准库之外看到了类似的东西。这里发生了什么?在特定断言发生之前,接口实现的某些方法是否隐藏?为什么我不能在 rw 对象上调用 Hijack()

【问题讨论】:

  • “在特定转换发生之前一直隐藏”——请注意:Go 根本不进行转换,所以这不能解释为 :)
  • 将“cast”更改为“assertion”,我同意这不是强制转换。但是你怎么称呼bs := []byte("Hello world")?这不是将字符串转换为字节片吗?
  • 不,这不是选角。这是一种类型转换。如果不清楚,请阅读 here 的区别。

标签: go methods interface


【解决方案1】:

这些方法似乎是隐藏的,因为该函数采用接口类型。 http.ResponseWriter 的接口没有定义Hijack() 方法。这是在http.Hijacker 接口中定义的。一个具体类型可以实现多个接口。但是,即使将这种具体类型传递到其类型定义为接口的作用域中,也无法访问其他方法。因此,在问题示例中,执行类型断言以使 Hijack() 方法可用。

一些例子(playground):

package main

import (
    "fmt"
)

type Ship interface {
    Load(containers []string)
    Condition() []string
}

type Sea interface {
    Draft() int
}

// seaShip implements the Sea and Ship interfaces
type seaShip struct {
    containers []string
}

// Load is only part of the Ship interface
func (ss *seaShip) Load(containers []string) {
    ss.containers = append(ss.containers, containers...)
}

// Condition is only part of the Ship interface
func (ss *seaShip) Condition() []string {
    return ss.containers
}

// Draft is only part of the Sea interface
func (ss *seaShip) Draft() int {
    return len(ss.containers)
}

// Pirates is not defined in any interface and therefore can only be called on the concrete type
func (ss *seaShip) Pirates() string {
    return "Help!"
}

// NewShip returns an implementation of the Ship interface
func NewShip() Ship {
    return &seaShip{}
}

func main() {
    ship := NewShip()
    ship.Load([]string{"Beer", "Wine", "Peanuts"})
    fmt.Println(ship.Condition())
    // Won't compile, method is not part of interface!
    // fmt.Println(ship.Draft())

    // Assert to make Draft() available
    sea := ship.(Sea)
    fmt.Println(sea.Draft())

    // Won't compile, methods are not part of interface!
    // fmt.Println(sea.Condition())
    // fmt.Println(sea.Pirates())

    // Assert to the concrete type makes all methods available
    ss := sea.(*seaShip)
    fmt.Println(ss.Condition())
    fmt.Println(ss.Pirates())
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 2012-05-29
    • 1970-01-01
    相关资源
    最近更新 更多