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

+ldflags #73

Merged
merged 4 commits into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
+ [➡ Create 3D visualization of concurrency traces](#-create-3d-visualization-of-concurrency-traces)
- Generate Code
+ [➡ Generate `String` method for enum types](#-generate-string-method-for-enum-types)
+ [➡ Run `go:generate` in parallel](#-run-gogenerate-in-parallel)
- Refactoring
+ [➡ Replace symbol](#-replace-symbol)
- Errors
Expand All @@ -60,6 +61,7 @@
+ [➡ Disable inlining](#-disable-inlining)
+ [➡ Aggressive inlining](#-aggressive-inlining)
+ [➡ Manually disable or enable `cgo`](#-manually-disable-or-enable-cgo)
+ [➡ Include metadata in binary during compilation with `ldflags`](#-include-metadata-in-binary-during-compilation-with-ldflags)
- Binary
+ [➡ Make treemap breakdown of Go executable binary](#-make-treemap-breakdown-of-go-executable-binary)
- Documentation
Expand Down Expand Up @@ -792,6 +794,16 @@ Requirements
go install golang.org/x/tools/cmd/stringer@latest
```

### ➡ Run `go:generate` in parallel

Official Go team [encourages](https://github.com/golang/go/issues/20520) to run sequentially. However, in certain cituations, such as lots of mocks, parallelization helps a lot, ableit, you should consider including your generated files in git. The solution bellow spawns multiple processes, each per pkg.


```
grep -rnw "go:generate" -E -l "${1:-*.go}" . | xargs -L1 dirname | sort -u | xargs -P 8 -I{} go generate {}
```


## Refactoring

### ➡ Replace symbol
Expand Down Expand Up @@ -874,6 +886,29 @@ go build -gcflags="-l -l -l -l" .
Disable `cgo` with `CGO_ENABLED=0` and enable with `CGO_ENABLED=1`. If you don't, `cgo` may end-up being enabled or code dynamically linked if, for example, you use some `net` or `os` packages. You may want to disable `cgo` to improve performance, since complier and runtime would have easier job optimizing code. This also should reduce your image size, as you can have alpine image with less shared libraries.


### ➡ Include metadata in binary during compilation with `ldflags`

You can pass metadata through compiler to your binary. This is useulf for including things like git commit, database schema version, integrity hashes. Variables can only be strings.


```
go build -v -ldflags="-X 'main.Version=v1.0.0'"
go build -v -ldflags="-X 'my/pkg/here.Variable=some-string'"
```

```go
package main

var Version string

func main() {
// Version here has some value
...
}

```


## Binary

### ➡ Make treemap breakdown of Go executable binary
Expand Down
10 changes: 10 additions & 0 deletions page.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ groups:
example_content_url: source/stringer/stringer.go
requirements:
- go install golang.org/x/tools/cmd/stringer@latest
- title: Run `go:generate` in parallel
description: Official Go team [encourages](https://github.com/golang/go/issues/20520) to run sequentially. However, in certain cituations, such as lots of mocks, parallelization helps a lot, ableit, you should consider including your generated files in git. The solution bellow spawns multiple processes, each per pkg.
commands:
- grep -rnw "go:generate" -E -l "${1:-*.go}" . | xargs -L1 dirname | sort -u | xargs -P 8 -I{} go generate {}
- title: Refactoring
entries:
- title: Replace symbol
Expand Down Expand Up @@ -329,6 +333,12 @@ groups:
- go build -gcflags="-l -l -l -l" .
- title: Manually disable or enable `cgo`
description: Disable `cgo` with `CGO_ENABLED=0` and enable with `CGO_ENABLED=1`. If you don't, `cgo` may end-up being enabled or code dynamically linked if, for example, you use some `net` or `os` packages. You may want to disable `cgo` to improve performance, since complier and runtime would have easier job optimizing code. This also should reduce your image size, as you can have alpine image with less shared libraries.
- title: Include metadata in binary during compilation with `ldflags`
description: You can pass metadata through compiler to your binary. This is useulf for including things like git commit, database schema version, integrity hashes. Variables can only be strings.
commands:
- go build -v -ldflags="-X 'main.Version=v1.0.0'"
- go build -v -ldflags="-X 'my/pkg/here.Variable=some-string'"
example_content_url: source/ldflags-meta.go
- title: Binary
entries:
- title: Make treemap breakdown of Go executable binary
Expand Down
8 changes: 8 additions & 0 deletions source/ldflags-meta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

var Version string

func main() {
// Version here has some value
...
}