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

末鹿安然 提交于 2020-01-23 10:45:10

问题


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 have a folder structure \root\module1 \root\module2

I have a go.mod in each directory and I can access / use those modules from the \root directory

How can I access module2 from module1. The modules are not published anywhere ( nor do I want them to be ) - I just want to access them. Module 2 contains types / structs that I need to use in mondule1

Kind Regards Martin


回答1:


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"
)


来源:https://stackoverflow.com/questions/57940353/how-to-structure-golang-modules-and-project-structure-in-the-new-way

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!