Importing a package declared in the root directory of a module

117 views
Skip to first unread message

Victor Giordano

unread,
Apr 15, 2023, 3:31:12 PM4/15/23
to golang-nuts
Hi there! 

I was playing a little bit with modules and packages, regarding making projects.
And I'm struggling to use a package (non-main) declared in the root directory.. allow me to show an example:

This scenario,  I have project somewhere on my file system
.
├── cmd
│   └── main.go
├── go.mod
└── util.go

go.mod
module a_module_path

go 1.19


util.go
package util

func Abs(x int) int {
if x < 0 {
return -x
} else {
return x
}
}

And the problem appears when I try to use the package util (in the root directory of the module) within another package of the module...

cmd/main.go
package main

import (
"fmt"
"a_module_path/util" // this doesn't works
"a_module_path/../util" // this attemp neither (and as relative import paths are not supported in module mode i guess is a no-go)
)

func main() {
fmt.Println(util.Abs(-2))
}

The question, as you may predict, is ¿If there any way to make this work?
I do not want to create a folder named util (or whatever) and place there the util package.

Thanks for the reading and the patience!
Keep rocking code! 

Sean Liao

unread,
Apr 15, 2023, 3:34:01 PM4/15/23
to golang-nuts
import "a_module_path"

optionally rename it to make it clearer

import util "a_module_path"

- sean

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/05b29995-e451-4235-a613-8193d995efcdn%40googlegroups.com.

Victor Giordano

unread,
Apr 15, 2023, 3:40:55 PM4/15/23
to golang-nuts
Thanks Sean!!
That makes sense for me!

But i guess I must import with an alias as import without an alias doesn't works, right?

<user>@<machine>:<the_project_folder>/cmd$ go build .
# a_module_path/cmd
./main.go:4:2: imported and not used: "a_module_path" as util
./main.go:9:14: undefined: a_module_path


main.go
package main

import (
"a_module_path"
"fmt"
)

func main() {
fmt.Println(a_module_path.Abs(-2))
}

I mean... i'm actually importing it without utill.. so the message kind of confuse me.

Thanks again.

Sean Liao

unread,
Apr 15, 2023, 3:50:30 PM4/15/23
to golang-nuts
the import path specifies the location to find a package, but the actual identifier used is the one in the package declaration, so

import "a_module_path"

var foo = util.Foo

- sean

Victor Giordano

unread,
Apr 15, 2023, 3:56:04 PM4/15/23
to Sean Liao, golang-nuts
Thanks again!

You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/9LtHHkjGsGQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/CAGabyPqs%2BHUse780M-CdYaAYR%3D8qSdWotYxE5QSqJZwfSD65SA%40mail.gmail.com.


--
V
Reply all
Reply to author
Forward
0 new messages