V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
thomaswang
V2EX  ›  Go 编程语言

golang struct

  •  
  •   thomaswang · 2018-09-21 11:22:04 +08:00 · 2414 次点击
    这是一个创建于 2035 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一个 struct 实现接口中的方法,是必须写在 struct 当前文件里面,还是必须写在 struct 当前文件所在的 package 里面,还是可以随意位置

    在其他文件这个 struct 对象调用方法,我用的 goland,点击这个方法总是跳到 interface,不好找这个方法的实现

    我说的不清楚,大家明白我的意思吗

    4 条回复    2018-09-21 12:25:24 +08:00
    markx
        1
    markx  
       2018-09-21 11:24:09 +08:00
    应该写在 package 里面就可以,可以在不同的文件里。
    webluoye
        2
    webluoye  
       2018-09-21 11:47:10 +08:00
    一个包里面就行,不限制文件。
    zhs227
        3
    zhs227  
       2018-09-21 11:49:33 +08:00
    package 里面,可以任意位置。goland 跳到接口是因为当前推断就是 interface。比如函数参数的传入类型是 interface。
    lbp0200
        4
    lbp0200  
       2018-09-21 12:25:24 +08:00
    package main

    type animal interface {
    fuck()
    }

    type human struct {
    age int
    }

    func (h human) fuck() {
    println("oh my yes")
    }

    func main() {
    var p animal
    p = human{age: 18}
    p.fuck()
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5637 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 01:37 · PVG 09:37 · LAX 18:37 · JFK 21:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.