Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include buildable library and command example code #72

Open
mleonhard opened this issue Jul 9, 2020 · 0 comments
Open

Include buildable library and command example code #72

mleonhard opened this issue Jul 9, 2020 · 0 comments

Comments

@mleonhard
Copy link

mleonhard commented Jul 9, 2020

Thanks for showing how to name the directories. I also need to know how to:

  1. Add library code
  2. Import library code into a binary
  3. Build a binary
  4. Perform offline builds

How about adding some example code? Ideally, this would be a complete buildable repository with short .go files and Makefile that show best practices for building, running tests, linting, etc.

It would be great to incorporate the solution for golang/go#37554 , to save folks from having to understand 27 pages of technical discussion.

These things are not at all obvious. For example, the obvious command go build ... does not produce any binaries. Running go build -v ... produces no output and go build -vvv ... doesn't work. And if I manage to produce a binary with go build cmd/example/example.go, the corresponding go clean does not delete it. Please put the appropriate commands in Makefile.

Here's example code to get started:

// go.mod
// Allow others to import this module into their projects or use `go install`:
module github.com/org1/example
// For private code:
//module _/example

go 1.14
// lib1/lib1.go
package lib1

import "fmt"

func Func1() {
	fmt.Println("lib1.Func1()")
}
// pkg/lib2/lib2.go
package lib2

import "fmt"

func Func2() {
	fmt.Println("lib2.Func2()")
}
// cmd/example/example.go
package main

import (
	"github.com/org1/example/lib1"
	"github.com/org1/example/pkg/lib2"
	// "_/example/lib1"
	// "_/example/pkg/lib2"
)

func main() {
	lib1.Func1()
	lib2.Func2()
}
# Makefile
#GOCACHE=output/cache/ fails with "GOCACHE is not an absolute path"
GO_BUILD=go build -pkgdir output/pkg/ -i -o output/ -v -trimpath
buid: mkoutput
	${GO_BUILD} ./...

mkoutput:
	mkdir -p output/ output/pkg/

clean:
	rm -rf output/
$ make
mkdir -p output/ output/pkg/
go build -pkgdir output/pkg/ -i -o output/ -v -trimpath ./...
runtime/internal/sys
math/bits
internal/race
unicode/utf8
unicode
runtime/internal/atomic
internal/cpu
sync/atomic
runtime/internal/math
internal/testlog
internal/bytealg
math
runtime
internal/reflectlite
sync
errors
sort
internal/oserror
io
strconv
syscall
reflect
internal/syscall/execenv
internal/syscall/unix
time
internal/poll
os
internal/fmtsort
fmt
github.com/org1/example/lib1
github.com/org1/example/pkg/lib2
github.com/org1/example/cmd/example
$ ls -alF output/
total 4240
drwxr-xr-x   4 user  staff      128 Jul  9 17:03 ./
drwxr-xr-x   8 user  staff      256 Jul  9 17:03 ../
-rwxr-xr-x   1 user  staff  2170120 Jul  9 17:03 example*
drwxr-xr-x  20 user  staff      640 Jul  9 17:03 pkg/
$ ./output/example 
lib1.Func1()
lib2.Func2()
$ make clean
rm -rf output/
$

Edit: Added alternative _/example module name. Ran go clean -cache and now build command shows output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant