在服务器HTTP中出现错误,但我的代码中没有错误,为什么?

huangapple go评论119阅读模式
英文:

Erroring in serverHTTP but not in my code, why?

问题

第一段代码:http://play.golang.org/p/OEDetydMbW

第二段代码:http://play.golang.org/p/QZIrWALAm_

有人可以解释一下为什么第一段代码没有报错吗?我原本期望会出现一个错误,提示“缺少CreateTable方法”。

英文:

First code: http://play.golang.org/p/OEDetydMbW

Second code: http://play.golang.org/p/QZIrWALAm_

Can somebody explain me why I am not getting error on First code, I was expecting to error out stating missing CreateTable method.

答案1

得分: 1

你指定了一个名为Abc的接口,其中包含了方法CreateTable,但是你的变量都没有实际上是Abc接口类型的。

稍微修改一下的版本会引发你所寻找的错误:
http://play.golang.org/p/ETdexzPYaM

  1. package main
  2. import "log"
  3. // Abc asdlkfjaslf as
  4. type Abc interface {
  5. CreateTable(a, b)
  6. }
  7. type a int
  8. type b int
  9. // Def klajsdlfkjaslfd
  10. type Def int
  11. // // CreateTable laksjdfljasfdl
  12. // func (d *Def) CreateTable() {
  13. // log.Println("inside Def CreateTable....")
  14. // }
  15. func main() {
  16. var m1 Abc = Def(5)
  17. log.Println("inside main %d", m1)
  18. }
英文:

You specify an interface Abc with the method CreateTable but none of your variable are actually of type interface Abc
This slightly modified version will bring the error you seek:
http://play.golang.org/p/ETdexzPYaM

  1. package main
  2. import "log"
  3. // Abc asdlkfjaslf as
  4. type Abc interface {
  5. CreateTable(a, b)
  6. }
  7. type a int
  8. type b int
  9. // Def klajsdlfkjaslfd
  10. type Def int
  11. // // CreateTable laksjdfljasfdl
  12. // func (d *Def) CreateTable() {
  13. // log.Println("inside Def CreateTable....")
  14. // }
  15. func main() {
  16. var m1 Abc = Def(5)
  17. log.Println("inside main %d", m1)
  18. }

huangapple
  • 本文由 发表于 2015年1月24日 09:16:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/28121260.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定