is there a bullet-proof description of how to use go locally: no domain, no github, ..

130 views
Skip to first unread message

andreas graeper

unread,
Jun 8, 2026, 11:51:01 AM (yesterday) Jun 8
to golang-nuts
xxx/main.go  
 # 'package main'
> go run main.go 
> go build .     (now need for go.mod)
> go run 
but works

xxx/
  cmd/    main.go and app.go (both 'package main') 
build and run work

now
 xxx/internal/a/a.go   (package a)
 xxx/internal/b/b.go   (package b) 

i would like to build package a and b and use them in package main

i used `go work init`  -> go.work 
go use cmd
go use internal/a
go use internal/b 
but no chance. xxx> go build . (no Go files, though go.mod, go.sum go.work exists)
no good error message !

gcc -o a.o -c a.c         
gcc -o b.o -c b.c         
gcc -o m m.c a.o b.o
this should be possible in go, too ?! 

thanks in advance, andi

Brian Candler

unread,
Jun 8, 2026, 12:52:58 PM (yesterday) Jun 8
to golang-nuts
Yes, go works locally. What do you mean by "no domain"?  Module names are arbitrary. For example, "go mod init wombat" works just fine.

> i would like to build package a and b and use them in package main

You don't need workspaces for that.

> no good error message !

If you don't show the error message, we can't help you.

I can't really see what the problem is. You have some issue with workspaces, I think. Why do you need workspaces?

Brian Candler

unread,
Jun 8, 2026, 1:04:46 PM (yesterday) Jun 8
to golang-nuts
Here's an incus container with no networking which demonstrates it working.

root@zzz:~# ping 8.8.8.8
ping: connect: Network is unreachable
root@zzz:~# go mod init wombat
go: creating new go.mod: module wombat
go: to add module requirements and sums:
go mod tidy
root@zzz:~# vi main.go
root@zzz:~# mkdir -p internal/a internal/b
root@zzz:~# vi internal/a/main.go
root@zzz:~# vi internal/b/main.go
root@zzz:~# go mod tidy
root@zzz:~# go fmt ./...
root@zzz:~# go build .
root@zzz:~# ls -l
total 1796
drwxr-xr-x 3 root root    4096 Jun  8 16:58 go
-rw-r--r-- 1 root root      25 Jun  8 16:58 go.mod
drwxr-xr-x 4 root root    4096 Jun  8 16:58 internal
-rw-r--r-- 1 root root     103 Jun  8 17:01 main.go
-rwxr-xr-x 1 root root 1821783 Jun  8 17:01 wombat
root@zzz:~# ./wombat
Hello, World!


Here are the source files:

==> main.go <==
package main

import "wombat/internal/a"
import "wombat/internal/b"

func main() {
a.Say()
b.Say()
}

==> internal/a/main.go <==
package a

func Say() {
print("Hello, ")
}

==> internal/b/main.go <==
package b

func Say() {
print("World!\n")
}

Ian Davis

unread,
Jun 8, 2026, 1:49:19 PM (yesterday) Jun 8
to golan...@googlegroups.com
You need to initialize your module

go mod init xyz

Then import your packages that are now under the module xyz.

package main

import "xyz/internal/a"
import "xyz/internal/b"

func main() {
        println("hey")
        println("a.A=",a.A)
        println("b.B=",b.B)

}

Your module can be called whatever you like so long as it doesn't clash with the standard library. 
--
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.

Robert Solomon

unread,
Jun 8, 2026, 7:59:13 PM (19 hours ago) Jun 8
to golang-nuts
I asked this question several years ago.  The answer I got helped me enough that I still use it.  
cd ~/go
In this directory I have my directory I call src.  All my code lives under src
cd src
go mod init src

Now in all my import lines I can use 
import "src/mymodule"

When I compile this, I use
go install ./myprogram

I haven't needed to learn workspaces.

Brian Candler

unread,
2:57 AM (12 hours ago) 2:57 AM
to golang-nuts
Be careful with that directory: ~/go is used by go itself for special purposes (e.g. downloading third-party modules, and binaries you install with "go install")

It's safer to make a completely separate directory for your own code, which is not part of that tree.

Robert Solomon

unread,
7:33 AM (7 hours ago) 7:33 AM
to golang-nuts
In my ~/go, there's
~/go/src
~/go/bin
~/go/pkg

They play nice w/ one another.  I haven't had any problems in 10 yrs.  In fact, the earlier GOBIN process recommended this directory layout.
What potential difficulties are you talking about?

--rob

Jason E. Aten

unread,
2:18 PM (1 hour ago) 2:18 PM
to golang-nuts
It is fine to use ~/go/src/. In fact it was required under GOPATH mode for about a decade of Go's life. So don't worry.
Typically if I have a module import path of "github.com/glycerine/gown" for example, I still put the source under ~/go/src/github.com/glycerine/gown/; since my GOPATH=/Users/jaten/go

Reply all
Reply to author
Forward
0 new messages