How to Structure Golang Modules and Project structure in the New way

前端 未结 1 545
青春惊慌失措
青春惊慌失措 2021-01-12 16:02

It seems that the way modules are used since 1.11 has changed , and I am trying to understand how to reference a module / package from another directory.

Lets say I

相关标签:
1条回答
  • 2021-01-12 16:25

    OLD WAY

    Go modules have to be placed in GOPATH for be used.

    When i start a new go project, i usually create a folder into the gopath

    cd $GOPATH
    ls
    

    Here you find 3 folder

    bin  pkg  src
    ls src
    >code.cloudfoundry.org  github.com  github.ibm.com  golang.org  gopkg.in  go.uber.org  honnef.co  winterdrache.de
    

    Into src, there are the code that you retrieve using 'go get' command.

    Everything that is here can be imported(/exported) into your software.

    Assume this test project:

    github.ibm.com/
    └── Alessio-Savi
        └── GoLog-Viewer
            ├── conf
            │   ├── dev.json
            │   └── test.json
            ├── database
            │   ├── cloudant
            │   │   └── cloudant.go
            │   └── db2
            │       └── db2.go
            ├── datastructure
            │   └── datastructures.go
            ├── GinProva.go
            ├── README.md
            ├── request
            │   └── request.go
            └── resources
                └── template01.html
    

    NOTE: Data structure are saved in a go file in a properly directory for avoid circle-import

    You can import the datastructures.go (or another file that you need) using the following import statement

    package mypackage
    
    import(
        "github.ibm.com/Alessio-Savi/GoLog-Viewer/datastructure"
    )
    

    In other file (in the same project as in other) you can simply use the package and let the IDE help you (due to the fact the the module/project is in GOPATH)

    New way

    In order to create a new module, you can use the new go module init gotool command.

    A common way for create a new module, in case of public source code, is the follwing:

    go mod init github.com/username/modulename
    

    This will generate two file:

    1. go.mod
    2. go.sum

    The go.mod file will contain every library/external golang code necessary to run your module. The go.sum file will contain the hash of the library.

    I'll use for example my little general purpose library, called GoGPUtils.

    mkdir GoGPUtils
    cd $_
    go mod init github.com/alessiosavi/GoGPUtils
    

    Now, you can insert the library that you need in your code in the go.mod library. Assume that you need the ahocorasick implementation for work with string search, the go.mod file will contains the following content:

    module github.com/alessiosavi/GoGPUtils
    
    go 1.13
    
    require (
        github.com/alessiosavi/ahocorasick v0.0.3
        golang.org/x/tools v0.0.0-20191031220737-6d8f1af9ccc0 // indirect
    )
    

    In the require section, there are the list of package needed. Now you can import the ahocorasick library in your code as following:

    import (
        ahocorasick "github.com/alessiosavi/ahocorasick"
    )
    
    0 讨论(0)
提交回复
热议问题