Cannot use custom package

hi, i’m stuck with a really basic stuff. it’s nubs question.
i want just to create custom package without external repo.

this is my folder structure outside ~/go/src folder.

go-packages/
  +utility
     -helper.go
  -main.go
package main
import (
  "fmt"
  "go-packages/utility"
)
func main() {
     fmt.Println(utility.Testme())
}

and helper.go

package utility
func Testme() int {
return 1
}

go run always produces an error
main.go:5:2: package go-packages/utility is not in GOROOT (/usr/local/go/src/go-packages/utility)
i tried to add mod file: go mod init example dot com/go-packages/, does not help.
What’s i’m doing wrong?

There are lots of sources i’ve watched/read where i tried an example from.
blog.golang.org/using-go-modules

and many others.

Hi, @anthonys, can you edit your post and try saving again? I can’t quite tell the folder structure because I think maybe your indentation was lost. Can you wrap it in backticks (`) like this:

```
code
```

hi @skillian, thx for the tip!

@anthonys Check out this post: Organize local code in packages using Go modules

I think you need a go.mod file in your go-packages folder defining your module name as "go-packages" and then your import statement should work.

@skillian
actually i have that in the go-packages folder
go.mod

module example.com/go-packages

go 1.14

@anthonys In that case, say import "example.com/go-packages/utility"

@skillian you saved my second day! thank you so much!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.