Skip to content

Commit 5d2bcaf

Browse files
Set up minimal Go project (#2)
A simple "Hello world" project. We use the popular directory structure of putting all binaries in `cmd/` and library code in `pkg/`. To keep the example simple, the binaries are simply hello world in English and Spanish. Demos internal and external tests, but not benchmarks and examples (even though these are supported).
1 parent e26cee9 commit 5d2bcaf

File tree

14 files changed

+297
-0
lines changed

14 files changed

+297
-0
lines changed

.github/workflows/pants.yaml

+14
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,24 @@ jobs:
2929
~/.cache/pants/lmdb_store
3030
~/.cache/pants/named_caches
3131
key: ${{ runner.os }}-
32+
- name: Install Go
33+
uses: actions/setup-go@v2
34+
with:
35+
go-version: 1.17.1
3236
- name: Bootstrap Pants
3337
run: ./pants --version
3438
- name: Check Pants config files
3539
run: ./pants tailor --check update-build-files --check
40+
- name: Lint and compile
41+
run: ./pants lint check '::'
42+
- name: Test
43+
run: ./pants test '::'
44+
- name: Package / Run
45+
run: |
46+
# We also smoke test that our release process will work by running `package`.
47+
./pants package ::
48+
./pants run cmd/greeter_en:
49+
./pants run cmd/greeter_es:
3650
- name: Upload Pants log
3751
uses: actions/upload-artifact@v2
3852
with:

BUILD

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Copyright 2021 Pants project contributors.
2+
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3+
4+
# This target teaches Pants about our Go module, including its third-party modules.
5+
go_mod(
6+
name="mod",
7+
)

README.md

+114
Original file line numberDiff line numberDiff line change
@@ -1 +1,115 @@
11
# example-golang
2+
3+
An example repository to demonstrate Pants's experimental Golang support.
4+
5+
See [] for some unique benefits Pants brings to Golang repositories, and see
6+
[pantsbuild.org/docs/go-overview](https://www.pantsbuild.org/v2.8/docs/go-overview) for much more detailed
7+
documentation.
8+
9+
This is only one possible way of laying out your project with Pants. See
10+
[pantsbuild.org/docs/source-roots#examples](https://www.pantsbuild.org/docs/source-roots#examples)
11+
for some other example layouts.
12+
13+
Note: for now, Pants only supports repositories using a single `go.mod`. Please comment on
14+
[#13114](https://github.com/pantsbuild/pants/issues/13114) if you need support for greater
15+
than one `go.mod` so that we can prioritize adding support.
16+
17+
# Running Pants
18+
19+
You run Pants goals using the `./pants` wrapper script, which will bootstrap the
20+
configured version of Pants if necessary.
21+
22+
# Goals
23+
24+
Pants commands are called _goals_. You can get a list of goals with
25+
26+
```
27+
./pants help goals
28+
```
29+
30+
Most goals take arguments to run on. To run on a single directory, use the directory name with
31+
`:` at the end. To recursively run on a directory and all its subdirectories, add `::` to the
32+
end.
33+
34+
For example:
35+
36+
```
37+
./pants lint cmd: internal::
38+
```
39+
40+
You can run on all changed files:
41+
42+
```
43+
./pants --changed-since=HEAD lint
44+
```
45+
46+
You can run on all changed files, and any of their "dependees":
47+
48+
```
49+
./pants --changed-since=HEAD --changed-dependees=transitive test
50+
```
51+
52+
# Example Goals
53+
54+
Try these out in this repo!
55+
56+
## Run Gofmt
57+
58+
```
59+
./pants fmt :: # Format all packages.
60+
./pants fmt cmd/greeter_en: # Format only this package.
61+
./pants lint pkg:: # Check that all packages under `pkg` are formatted.
62+
```
63+
64+
## Check compilation
65+
66+
```
67+
./pants check :: # Compile all packages.
68+
./pants check cmd/greeter_en: # Compile only this package and its transitive dependencies.
69+
```
70+
71+
## Run tests
72+
73+
```
74+
./pants test :: # Run all tests in the repository.
75+
./pants test pkg/uuid: # Run all the tests in this package.
76+
./pants test pkg/uuid: -- -run TestGenerateUuid # Run just this one test.
77+
```
78+
79+
## Create a binary file
80+
81+
Writes the result to the `dist/` folder.
82+
83+
```
84+
./pants package cmd/greeter_en:
85+
./pants package cmd:: # Create all binaries.
86+
```
87+
88+
## Run a binary
89+
90+
```
91+
./pants run cmd/greeter_en:
92+
./pants run cmd/greeter_es: -- --help
93+
```
94+
95+
## Determine dependencies
96+
97+
```
98+
./pants dependencies cmd/greeter_en:
99+
./pants dependencies --transitive cmd/greeter_en:
100+
```
101+
102+
## Determine dependees
103+
104+
That is, find what code depends on a particular package(s).
105+
106+
```
107+
./pants dependees pkg/uuid:
108+
./pants dependees --transitive pkg/uuid:
109+
```
110+
111+
## Count lines of code
112+
113+
```
114+
./pants count-loc '**/*'
115+
```

cmd/greeter_en/BUILD

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright 2021 Pants project contributors.
2+
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3+
4+
# This target allows us to use `./pants run` and `./pants package` on this `main` Go package.
5+
#
6+
# You can optionally set the field `output_path="greeter_en"`, for example, for the binary's name
7+
# to be different when running `./pants package`.
8+
go_binary(
9+
name="bin",
10+
)

cmd/greeter_en/main.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2021 Pants project contributors.
2+
// Licensed under the Apache License, Version 2.0 (see LICENSE).
3+
4+
package main
5+
6+
import (
7+
"fmt"
8+
"github.com/pantsbuild/example-golang/pkg/greeter"
9+
"github.com/spf13/pflag"
10+
"os"
11+
)
12+
13+
const version string = "0.1.0"
14+
15+
func main() {
16+
versionOpt := pflag.BoolP("version", "V", false, "print the version and exit")
17+
pflag.Parse()
18+
19+
if *versionOpt {
20+
fmt.Println(version)
21+
os.Exit(0)
22+
}
23+
24+
fmt.Println(greeter.GreetEnglish("Pantsbuild"))
25+
}

cmd/greeter_es/BUILD

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Copyright 2021 Pants project contributors.
2+
# Licensed under the Apache License, Version 2.0 (see LICENSE).
3+
4+
# This target allows us to use `./pants run` and `./pants package` on this `main` Go package.
5+
#
6+
# You can optionally set the field `output_path="greeter_es"`, for example, for the binary's name
7+
# to be different when running `./pants package`.
8+
go_binary(
9+
name="bin",
10+
)

cmd/greeter_es/main.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2021 Pants project contributors.
2+
// Licensed under the Apache License, Version 2.0 (see LICENSE).
3+
4+
package main
5+
6+
import (
7+
"fmt"
8+
"github.com/pantsbuild/example-golang/pkg/greeter"
9+
"github.com/spf13/pflag"
10+
"os"
11+
)
12+
13+
const version string = "0.1.0"
14+
15+
func main() {
16+
versionOpt := pflag.BoolP("version", "V", false, "imprimir la versión y salir")
17+
pflag.Parse()
18+
19+
if *versionOpt {
20+
fmt.Println(version)
21+
os.Exit(0)
22+
}
23+
24+
fmt.Println(greeter.GreetSpanish("Pantsbuild"))
25+
}

go.mod

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Copyright 2021 Pants project contributors.
2+
// Licensed under the Apache License, Version 2.0 (see LICENSE).
3+
4+
module github.com/pantsbuild/example-golang
5+
6+
go 1.17
7+
8+
require (
9+
github.com/google/uuid v1.3.0
10+
github.com/spf13/pflag v1.0.5
11+
)

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
2+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
3+
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
4+
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=

pants.toml

+4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33

44
[GLOBAL]
55
pants_version = "2.8.0rc3"
6+
backend_packages = ["pants.backend.experimental.go"]
67

78
[anonymous-telemetry]
89
enabled = true
910
repo_id = "AE018A26-213E-4B18-99A6-6923EC1E16DE"
11+
12+
[golang]
13+
expected_version = "1.17"

pkg/greeter/greet.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2021 Pants project contributors.
2+
// Licensed under the Apache License, Version 2.0 (see LICENSE).
3+
4+
package greeter
5+
6+
import (
7+
"fmt"
8+
"github.com/pantsbuild/example-golang/pkg/uuid"
9+
)
10+
11+
func GreetEnglish(name string) string {
12+
return fmt.Sprintf(
13+
"Hello %s!\n\nHere's a UUID to brighten your day: %s",
14+
name,
15+
uuid.Generate(),
16+
)
17+
}
18+
19+
func GreetSpanish(name string) string {
20+
return fmt.Sprintf(
21+
"¡Hola %s!\n\nEres muy única, así que te regalamos un UUID: %s",
22+
name,
23+
uuid.Generate(),
24+
)
25+
}

pkg/greeter/greet_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2021 Pants project contributors.
2+
// Licensed under the Apache License, Version 2.0 (see LICENSE).
3+
4+
package greeter_test // That is, an "external test"
5+
6+
import (
7+
"github.com/pantsbuild/example-golang/pkg/greeter"
8+
"strings"
9+
"testing"
10+
)
11+
12+
func TestEnglish(t *testing.T) {
13+
result := greeter.GreetEnglish("testing")
14+
if !strings.HasPrefix(result, "Hello testing!") {
15+
t.Fail()
16+
}
17+
}
18+
19+
func TestSpanish(t *testing.T) {
20+
result := greeter.GreetSpanish("testing")
21+
if !strings.HasPrefix(result, "¡Hola testing!") {
22+
t.Fail()
23+
}
24+
}

pkg/uuid/uuid_gen.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Copyright 2021 Pants project contributors.
2+
// Licensed under the Apache License, Version 2.0 (see LICENSE).
3+
4+
package uuid
5+
6+
import "github.com/google/uuid"
7+
8+
func Generate() string {
9+
return uuid.NewString()
10+
}

pkg/uuid/uuid_gen_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2021 Pants project contributors.
2+
// Licensed under the Apache License, Version 2.0 (see LICENSE).
3+
4+
package uuid
5+
6+
import "testing"
7+
8+
func TestGenerateUuid(t *testing.T) {
9+
uuid1 := Generate()
10+
uuid2 := Generate()
11+
if uuid1 == uuid2 {
12+
t.Fail()
13+
}
14+
}

0 commit comments

Comments
 (0)