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

1

12

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

Spinney answered 15/9, 2019 at 0:57 Comment(3)
See github.com/golang/go/wiki/…Forrester
Thanks @CeriseLimón - but I still don't get it. If I put one go.mod in the root directory - I am expecting to access a package using import "mymodule/mysubdir/mypackage" but it's not workingSpinney
Follow the example linked from the wiki page. Create a go.mod file in each module. Change "hello" to "module1" and "goodbye" to "module2" to match the code in question. Replace "example.com/me/" with whatever you want or use it as is.Forrester
V
8

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)

enter image description here

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"
)
Victimize answered 15/9, 2019 at 13:54 Comment(4)
Apparently not any more melvinvivas.com/go-version-1-11-modules/….Spinney
Updated with the new go mod initVictimize
I have 2 folders inside a root folder, module1 and module2 when i initialized both with go mod, and used the 2nd one inside first one, i couldn't get it, package x is not found in GOROOTProtolithic
Have you insert the second dependencies into the first go.mod file? If the project is open source, please share the linkVictimize

© 2022 - 2024 — McMap. All rights reserved.