How to import local packages without gopath

后端 未结 8 1924
有刺的猬
有刺的猬 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条回答
  • 2020-12-07 08:07

    Create some files like this:

    addition/aaa.go
    bbb.go
    

    The folder name will be one of your Module's packages. The file names can be whatever you want. Here is example package file:

    package addition
    
    func Add(n, n2 int) int {
       return n + n2
    }
    

    and the Module file:

    package main
    import "arithmetic/addition"
    
    func main() {
       n := addition.Add(1, 2)
       println(n)
    }
    

    Now initialize and build your module:

    go mod init arithmetic
    go build
    

    https://golang.org/doc/code.html

    0 讨论(0)
  • 2020-12-07 08:14

    There's no such thing as "local package". The organization of packages on a disk is orthogonal to any parent/child relations of packages. The only real hierarchy formed by packages is the dependency tree, which in the general case does not reflect the directory tree.

    Just use

    import "myproject/packageN"
    

    and don't fight the build system for no good reason. Saving a dozen of characters per import in any non trivial program is not a good reason, because, for example, projects with relative import paths are not go-gettable.

    The concept of import paths have some important properties:

    • Import paths can be be globally unique.
    • In conjunction with GOPATH, import path can be translated unambiguously to a directory path.
    • Any directory path under GOPATH can be unambiguously translated to an import path.

    All of the above is ruined by using relative import paths. Do not do it.

    PS: There are few places in the legacy code in Go compiler tests which use relative imports. ATM, this is the only reason why relative imports are supported at all.

    0 讨论(0)
  • 2020-12-07 08:18

    Go dependency management summary:

    • vgo if your go version is: x >= go 1.11
    • dep or vendor if your go version is: go 1.6 >= x < go 1.11
    • Manually if your go version is: x < go 1.6

    Edit 3: Go 1.11 has a feature vgo which will replace dep.

    To use vgo, see Modules documentation. TLDR below:

    export GO111MODULE=on
    go mod init
    go mod vendor # if you have vendor/ folder, will automatically integrate
    go build
    

    This method creates a file called go.mod in your projects directory. You can then build your project with go build. If GO111MODULE=auto is set, then your project cannot be in $GOPATH.


    Edit 2: The vendoring method is still valid and works without issue. vendor is largely a manual process, because of this dep and vgo were created.


    Edit 1: While my old way works it's not longer the "correct" way to do it. You should be using vendor capabilities, vgo, or dep (for now) that are enabled by default in Go 1.6; see. You basically add your "external" or "dependent" packages within a vendor directory; upon compilation the compiler will use these packages first.


    Found. I was able import local package with GOPATH by creating a subfolder of package1 and then importing with import "./package1" in binary1.go and binary2.go scripts like this :

    binary1.go

    ...
    import (
            "./package1"
          )
    ...
    

    So my current directory structure looks like this:

    myproject/
    ├── binary1.go
    ├── binary2.go
    ├── package1/
    │   └── package1.go
    └── package2.go
    

    I should also note that relative paths (at least in go 1.5) also work; for example:

    import "../packageX"
    
    0 讨论(0)
  • 2020-12-07 08:18

    To add a "local" package to your project, add a folder (for example "package_name"). And put your implementation files in that folder.

    src/github.com/GithubUser/myproject/
     ├── main.go
     └───package_name
           └── whatever_name1.go
           └── whatever_name2.go
    

    In your package main do this:

    import "github.com/GithubUser/myproject/package_name"
    

    Where package_name is the folder name and it must match the package name used in files whatever_name1.go and whatever_name2.go. In other words all files with a sub-directory should be of the same package.

    You can further nest more subdirectories as long as you specify the whole path to the parent folder in the import.

    0 讨论(0)
  • 2020-12-07 08:29

    Since the introduction of go.mod , I think both local and external package management becomes easier. Using go.mod, it is possible to have go project outside the GOPATH as well.

    Import local package:

    Create a folder demoproject and run following command to generate go.mod file

    go mod init demoproject

    I have a project structure like below inside the demoproject directory.

    ├── go.mod
    └── src
        ├── main.go
        └── model
            └── model.go
    

    For the demo purpose, insert the following code in the model.go file.

    package model
    
    type Employee struct {
        Id          int32
        FirstName   string
        LastName    string
        BadgeNumber int32
    }
    
    

    In main.go, I imported Employee model by referencing to "demoproject/src/model"

    package main
    
    import (
        "demoproject/src/model"
        "fmt"
    )
    
    func main() {
        fmt.Printf("Main Function")
    
        var employee = model.Employee{
            Id:          1,
            FirstName:   "First name",
            LastName:    "Last Name",
            BadgeNumber: 1000,
        }
        fmt.Printf(employee.FirstName)
    }
    

    Import external dependency:

    Just run go get command inside the project directory.

    For example:

    go get -u google.golang.org/grpc
    

    It should include module dependency in the go.mod file

    module demoproject
    
    go 1.13
    
    require (
        golang.org/x/net v0.0.0-20200114155413-6afb5195e5aa // indirect
        golang.org/x/sys v0.0.0-20200124204421-9fbb57f87de9 // indirect
        golang.org/x/text v0.3.2 // indirect
        google.golang.org/genproto v0.0.0-20200122232147-0452cf42e150 // indirect
        google.golang.org/grpc v1.26.0 // indirect
    )
    

    https://blog.golang.org/using-go-modules

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题