How to import local packages without gopath

后端 未结 8 2004
有刺的猬
有刺的猬 2020-12-07 07:58

I\'ve used GOPATH but for this current issue I\'m facing it does not help. I want to be able to create packages that are specific to a project:

         


        
8条回答
  •  旧时难觅i
    2020-12-07 08:30

    Perhaps you're trying to modularize your package. I'm assuming that package1 and package2 are, in a way, part of the same package but for readability you're splitting those into multiple files.

    If the previous case was yours, you could use the same package name into those multiples files and it will be like if there were the same file.

    This is an example:

    add.go

    package math
    
    func add(n1, n2 int) int {
       return n1 + n2
    }
    

    subtract.go

    package math
    
    func subtract(n1, n2 int) int {
        return n1 - n2
    }
    

    donothing.go

    package math
    
    func donothing(n1, n2 int) int {
        s := add(n1, n2)
        s = subtract(n1, n2)
        return s
    }
    

    I am not a Go expert and this is my first post in StackOveflow, so if you have some advice it will be well received.

提交回复
热议问题