Skip to content

Add gcflags and ldflags example #932

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

Merged
merged 1 commit into from
Jun 6, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
normal
debug
release
32 changes: 32 additions & 0 deletions examples/singleapp/gcflags_ldflags_debug_release_build/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# これは何?

Goにてデバッグビルドするときと、リリースビルドするときによく利用するフラグについて。

## デバッグ

```sh
go build -gcflags "all=-N -l" -o debug main.go
```

### オプションの意味

```
# gcflags の all=-N -l の意味 (goコンパイラに対しての指示) (go tool compile -help)
# all= は全てのパッケージが対象という意味
# -N は最適化無効という意味 (No optimization)
# -l はインライン化無効という意味 (No inlining)
```

## リリース

```sh
go build -ldflags "-s -w" -o release main.go
```

### オプションの意味

```
# ldflags の -s -w の意味 (リンカに対しての指示) (go tool link -help)
# -s はシンボルテーブル削除という意味
# -w はDWARF情報削除という意味(デバッグ情報)
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# https://taskfile.dev

version: '3'

vars:
RE1: main\.go.*inlining call.*$

tasks:
default:
cmds:
# gcflags の all=-m -N -l の意味 (goコンパイラに対しての指示) (go tool compile -help)
# all= は全てのパッケージが対象という意味
# -m はビルド時のコンパイラの詳細情報を出力せよという意味
# -N は最適化無効という意味 (No optimization)
# -l はインライン化無効という意味 (No inlining)
- cmd: go build -gcflags "all=-m -N -l" -o debug main.go 2>&1 | grep "{{.RE1}}"
ignore_error: true
- cmd: go build -gcflags "all=-m" -o normal main.go 2>&1 | grep "{{.RE1}}"
ignore_error: true
# ldflags の -s -w の意味 (リンカに対しての指示) (go tool link -help)
# -s はシンボルテーブル削除という意味
# -w はDWARF情報削除という意味(デバッグ情報)
- cmd: go build -gcflags "all=-m" -ldflags "-s -w" -o release main.go 2>&1 | grep "{{.RE1}}"
ignore_error: true
- ls -l {normal,debug,release} | awk 'NF>1 {print $5, $NF}'
63 changes: 63 additions & 0 deletions examples/singleapp/gcflags_ldflags_debug_release_build/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package main

import (
"io"
"log"
"sync"
"time"
)

func main() {
log.SetFlags(0)
log.SetOutput(io.Discard)

if err := run(); err != nil {
panic(err)
}
}

// この関数は最適化が有効な場合、直接 v*2 になる可能性があり
// インライン化が有効な場合、インライン化される可能性がある
func calc(v int) int {
v1 := v
v2 := 2
return v1 * v2
}

func run() error {
const (
COUNT = 10000000
WORKERS = 4
)
var (
ch = make(chan int)
wg sync.WaitGroup
)

// producer
wg.Add(1)
go func(ch chan<- int) {
defer wg.Done()
defer close(ch)

for i := range COUNT {
ch <- calc(i)
}
}(ch)

time.Sleep(10 * time.Millisecond)

// consumer
for range WORKERS {
wg.Add(1)
go func(ch <-chan int) {
for v := range ch {
log.Println(v)
}
}(ch)
}

wg.Done()

return nil
}
Loading