2015-01-19 03:51
浏览 10


Are there partial classes in Go?

Like the one here in C#?

I suppose there isn't any as you cannot partially declare structs.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douwei1921
    douwei1921 2015-01-19 04:20

    The method declarations for a type do not need to be in the same source file as the type declaration or other method declarations for the type. The method declarations do need to be in the same package as the type declaration.

    A type declaration cannot be split across files.

    Go does not have classes.

    点赞 评论
  • dongmuzhan4705
    dongmuzhan4705 2015-01-19 05:29

    in go you can have a method associated with any type within the same package in any file. take this small example of object foo with function Bar.

    package main
    import "fmt"
    type foo struct {} // put variables associated with the type here
    func ( /*f*/ foo) Bar() { // put a value infront of foo if u need to access any elements in the object
        // do something interesting
        fmt.Println("Method called :D")
    func main() {
        example := foo{}

    aslong as foo and bar declartions occur in the same package they can be placed in any file.

    i hope this demonstrated the desired functionality you are trying to implement / wondering if go can support.

    点赞 评论