-
Notifications
You must be signed in to change notification settings - Fork 15
Set up minimal Go project #2
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Copyright 2021 Pants project contributors. | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
# This target teaches Pants about our Go module, including its third-party modules. | ||
go_mod( | ||
name="mod", | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,115 @@ | ||
# example-golang | ||
|
||
An example repository to demonstrate Pants's experimental Golang support. | ||
|
||
See [] for some unique benefits Pants brings to Golang repositories, and see | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, waiting for the blog to have a stable link. Chicken and egg There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mm. Maybe just the http://blog.pantsbuild.org in the meantime then? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This repo isn't publicized yet, and planning on doing the blog on Monday. Will fix then a couple minutes before we publicize the blog |
||
[pantsbuild.org/docs/go-overview](https://www.pantsbuild.org/v2.8/docs/go-overview) for much more detailed | ||
documentation. | ||
|
||
This is only one possible way of laying out your project with Pants. See | ||
[pantsbuild.org/docs/source-roots#examples](https://www.pantsbuild.org/docs/source-roots#examples) | ||
for some other example layouts. | ||
|
||
Note: for now, Pants only supports repositories using a single `go.mod`. Please comment on | ||
[#13114](https://github.com/pantsbuild/pants/issues/13114) if you need support for greater | ||
than one `go.mod` so that we can prioritize adding support. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Greater than one sounds strange to me in this context.. "more than one"? |
||
|
||
# Running Pants | ||
|
||
You run Pants goals using the `./pants` wrapper script, which will bootstrap the | ||
configured version of Pants if necessary. | ||
|
||
# Goals | ||
|
||
Pants commands are called _goals_. You can get a list of goals with | ||
|
||
``` | ||
./pants help goals | ||
``` | ||
|
||
Most goals take arguments to run on. To run on a single directory, use the directory name with | ||
`:` at the end. To recursively run on a directory and all its subdirectories, add `::` to the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe mention that we are revisiting the dir spec syntax in 2.9? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on discussion this week, it's no longer a firm commitment that we'll fix this in 2.9 |
||
end. | ||
|
||
For example: | ||
|
||
``` | ||
./pants lint cmd: internal:: | ||
``` | ||
|
||
You can run on all changed files: | ||
|
||
``` | ||
./pants --changed-since=HEAD lint | ||
``` | ||
|
||
You can run on all changed files, and any of their "dependees": | ||
|
||
``` | ||
./pants --changed-since=HEAD --changed-dependees=transitive test | ||
``` | ||
|
||
# Example Goals | ||
|
||
Try these out in this repo! | ||
|
||
## Run Gofmt | ||
|
||
``` | ||
./pants fmt :: # Format all packages. | ||
./pants fmt cmd/greeter_en: # Format only this package. | ||
./pants lint pkg:: # Check that all packages under `pkg` are formatted. | ||
``` | ||
|
||
## Check compilation | ||
|
||
``` | ||
./pants check :: # Compile all packages. | ||
./pants check cmd/greeter_en: # Compile only this package and its transitive dependencies. | ||
``` | ||
|
||
## Run tests | ||
|
||
``` | ||
./pants test :: # Run all tests in the repository. | ||
./pants test pkg/uuid: # Run all the tests in this package. | ||
./pants test pkg/uuid: -- -run TestGenerateUuid # Run just this one test. | ||
``` | ||
|
||
## Create a binary file | ||
|
||
Writes the result to the `dist/` folder. | ||
|
||
``` | ||
./pants package cmd/greeter_en: | ||
./pants package cmd:: # Create all binaries. | ||
``` | ||
|
||
## Run a binary | ||
|
||
``` | ||
./pants run cmd/greeter_en: | ||
./pants run cmd/greeter_es: -- --help | ||
``` | ||
|
||
## Determine dependencies | ||
|
||
``` | ||
./pants dependencies cmd/greeter_en: | ||
./pants dependencies --transitive cmd/greeter_en: | ||
``` | ||
|
||
## Determine dependees | ||
|
||
That is, find what code depends on a particular package(s). | ||
|
||
``` | ||
./pants dependees pkg/uuid: | ||
./pants dependees --transitive pkg/uuid: | ||
``` | ||
|
||
## Count lines of code | ||
|
||
``` | ||
./pants count-loc '**/*' | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Copyright 2021 Pants project contributors. | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
# This target allows us to use `./pants run` and `./pants package` on this `main` Go package. | ||
# | ||
# You can optionally set the field `output_path="greeter_en"`, for example, for the binary's name | ||
# to be different when running `./pants package`. | ||
go_binary( | ||
name="bin", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The output is going to be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea. We lose a guarantee that the name is globally unique, but seems reasonable to change |
||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2021 Pants project contributors. | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/pantsbuild/example-golang/pkg/greeter" | ||
"github.com/spf13/pflag" | ||
"os" | ||
) | ||
|
||
const version string = "0.1.0" | ||
|
||
func main() { | ||
versionOpt := pflag.BoolP("version", "V", false, "print the version and exit") | ||
pflag.Parse() | ||
|
||
if *versionOpt { | ||
fmt.Println(version) | ||
os.Exit(0) | ||
} | ||
|
||
fmt.Println(greeter.GreetEnglish("Pantsbuild")) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Copyright 2021 Pants project contributors. | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
# This target allows us to use `./pants run` and `./pants package` on this `main` Go package. | ||
# | ||
# You can optionally set the field `output_path="greeter_es"`, for example, for the binary's name | ||
# to be different when running `./pants package`. | ||
go_binary( | ||
name="bin", | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2021 Pants project contributors. | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/pantsbuild/example-golang/pkg/greeter" | ||
"github.com/spf13/pflag" | ||
"os" | ||
) | ||
|
||
const version string = "0.1.0" | ||
|
||
func main() { | ||
versionOpt := pflag.BoolP("version", "V", false, "imprimir la versión y salir") | ||
pflag.Parse() | ||
|
||
if *versionOpt { | ||
fmt.Println(version) | ||
os.Exit(0) | ||
} | ||
|
||
fmt.Println(greeter.GreetSpanish("Pantsbuild")) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright 2021 Pants project contributors. | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
module github.com/pantsbuild/example-golang | ||
|
||
go 1.17 | ||
|
||
require ( | ||
github.com/google/uuid v1.3.0 | ||
github.com/spf13/pflag v1.0.5 | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= | ||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= | ||
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= | ||
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2021 Pants project contributors. | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
package greeter | ||
|
||
import ( | ||
"fmt" | ||
"github.com/pantsbuild/example-golang/pkg/uuid" | ||
) | ||
|
||
func GreetEnglish(name string) string { | ||
return fmt.Sprintf( | ||
"Hello %s!\n\nHere's a UUID to brighten your day: %s", | ||
name, | ||
uuid.Generate(), | ||
) | ||
} | ||
|
||
func GreetSpanish(name string) string { | ||
return fmt.Sprintf( | ||
"¡Hola %s!\n\nEres muy única, así que te regalamos un UUID: %s", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Roughly translates to:
Note that it's |
||
name, | ||
uuid.Generate(), | ||
) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2021 Pants project contributors. | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
package greeter_test // That is, an "external test" | ||
|
||
import ( | ||
"github.com/pantsbuild/example-golang/pkg/greeter" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestEnglish(t *testing.T) { | ||
result := greeter.GreetEnglish("testing") | ||
if !strings.HasPrefix(result, "Hello testing!") { | ||
t.Fail() | ||
} | ||
} | ||
|
||
func TestSpanish(t *testing.T) { | ||
result := greeter.GreetSpanish("testing") | ||
if !strings.HasPrefix(result, "¡Hola testing!") { | ||
t.Fail() | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// Copyright 2021 Pants project contributors. | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
package uuid | ||
|
||
import "github.com/google/uuid" | ||
|
||
func Generate() string { | ||
return uuid.NewString() | ||
} | ||
Comment on lines
+8
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A very silly function, but the point of the demo is to keep it simple. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2021 Pants project contributors. | ||
// Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
package uuid | ||
|
||
import "testing" | ||
|
||
func TestGenerateUuid(t *testing.T) { | ||
uuid1 := Generate() | ||
uuid2 := Generate() | ||
if uuid1 == uuid2 { | ||
t.Fail() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe link to https://www.pantsbuild.org/docs/using-pants-in-ci somewhere in here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On line 4 at the top of this file already