anthonys
(Anton S)
April 4, 2020, 10:15pm
1
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
VIDEO
and many others.
skillian
(Sean Killian)
April 4, 2020, 10:46pm
2
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
```
anthonys
(Anton S)
April 4, 2020, 10:51pm
3
hi @skillian , thx for the tip!
skillian
(Sean Killian)
April 4, 2020, 10:59pm
4
@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.
anthonys
(Anton S)
April 4, 2020, 11:01pm
5
@skillian
actually i have that in the go-packages folder
go.mod
module example.com/go-packages
go 1.14
skillian
(Sean Killian)
April 4, 2020, 11:18pm
6
@anthonys In that case, say import "example.com/go-packages/utility"
anthonys
(Anton S)
April 4, 2020, 11:25pm
7
@skillian you saved my second day! thank you so much!
system
(system)
Closed
July 3, 2020, 11:25pm
8
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.