-1

I am using go1.13.4 and below is my project structure:

src/types/type.go
src/utils/util.go
go.mod

In go.mod:

module example.com/graphql

go 1.13

require github.com/graph-gophers/graphql-go v0.0.0-20191031232829-adde0d0f76a3

In src/types/type.go:

package types

type USER struct {
   ...
}

In src/utils/util.go,

package utils
import (
    "example.com/graphql/types"
    "fmt"
    "io/ioutil"
    "os"
)

I got tan error when build the project:

$ go build ./...
src/utils/utils.go:4:2: cannot find module providing package example.com/graphql/types

I wonder why it can't find the package example.com/graphql/types? I am reading https://blog.golang.org/using-go-modules and I already set the module name in go.mod file at the root of my project.

icza
  • 389,944
  • 63
  • 907
  • 827
Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523
  • 1
    Please see [How to Write Go Code](https://golang.org/doc/code.html) (particularly the sections on import paths and your first library) and [Effective Go](https://golang.org/doc/effective_go.html) (particularly the section on names). – Adrian Nov 13 '19 at 14:33

1 Answers1

3

With your current layout, import path of types is example.com/graphql/src/types.

go.mod should be inside src if you have that structure. Or better would be to get rid of src. go.mod must be next to the types and utils folders.

icza
  • 389,944
  • 63
  • 907
  • 827