7

I have a main.go

package main

import (
    "context"
    "fmt"
    "log"

    model "model"

    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
)

func handler(...){
}

I try to import model which is in the directory model and the file is called model.go

It just contains:

package model

type xxx struct {
    xxx
}

I try to import this in the main but I have the error:

build: cannot load model: cannot find module providing package model
mealesbia
  • 845
  • 2
  • 12
  • 28

2 Answers2

9

If your module model is not local then you can use Tonys answer and it will work fine but if you are using this module locally then you will need to add the paths in your go.mod file.

So for example, Local module model contains only model.go which has the following content

package model

type Example struct {
    Name string
}

func (e *Example) Foo() string {
    return e.Name
}

For this local module must have have to init the module with the command go mod init model and the content of the ./model/go.mod will be

module model
go 1.13

In the main module in which you are importing this module you need to add the following line

require model v1.0.0
replace model v1.0.0 => {Absolute or relative path to the model module}

So, your main testing module's go.mod file will look like this

module main

require model v1.0.0
replace model v1.0.0 => ./model

go 1.13

By setting this up you can use this module in this test module with just import "model"

Hence when testing the module with the main method

package main

import (
    model "model"
)

func main() {
    example := model.Example{
        Name: "Hello World",
    }
    println(example.Foo())
}

The output will be

Hello World
Hamza Anis
  • 2,475
  • 1
  • 26
  • 36
  • it works but it's complicated while calling methods across the packages. Each package have to have go.mod declaration, better way is to put them in vendor folder – TomSawyer Jul 25 '21 at 14:57
4

If your go.mod looks like this:

module github.com/meakesbia/myproject

go 1.14

then you need to import the module package using the full module reference:

import "github.com/meakesbia/myproject/model"

If it's an entirely local project then replace github.com/meakesbia with the model name from go.mod e.g.:

module meakesbia/myproject

go 1.14
import "meakesbia/myproject/model"

You don't need to add a replace directive to the go.mod file unless you're making local changes to a module that is imported from e.g. github.

tonys
  • 3,855
  • 33
  • 39
  • 2
    Hi, it's fully local. So it's not on GitHub. – mealesbia Mar 29 '20 at 20:15
  • @tonys can you please check again whether the import without the `replace` directive will work fine in the case of _local project_? – Hamza Anis Mar 30 '20 at 00:54
  • @HamzaAnis, I double-checked before I edited the post and have just checked again. It seems fine - at least 'on my machine' :-) – tonys Mar 30 '20 at 16:03
  • You have this module under your `$GOPATH`? – Hamza Anis Mar 30 '20 at 17:19
  • @HamzaAnis: Nope - with Go modules you no longer use GOPATH, as per the official guide (https://blog.golang.org/using-go-modules) and sundry other blogs (e.g. https://blog.francium.tech/go-modules-go-project-set-up-without-gopath-1ae601a4e868). If it is set and/or you are using a 'src' directory structure that may be why you need a 'replace' directive in your go.mod. – tonys Mar 31 '20 at 00:46