如何覆盖匿名结构函数。为了阐明我的意思,请查看以下代码片段:package baseimport ( "fmt" "net/http")type Executer interface { Execute()}type Controller struct { }func (self *Controller) Execute() { fmt.Println("Hello Controller")}func (self *Controller) ServeHTTP(rw http.ResponseWriter, r *http.Request) { self.Execute()}现在我将控制器结构嵌入到测试结构中,也称为匿名package baseimport ( "fmt" "net/http" "net/http/httptest" "testing")type Test struct { Controller}func (self *Test) Execute() { fmt.Println("Hello Test")}func TestInheritance(t *testing.T) { ts := httptest.NewServer(&Test{}) defer ts.Close() http.Get(ts.URL)}作为输出,我有“Hello Controller”,但期望“Hello Test”。你可以看到上面的代码,我重新实现了execute函数,但它不起作用。
- 2 回答
- 0 关注
- 203 浏览
添加回答
举报
0/150
提交
取消