Skip to content

Commit e0d57bd

Browse files
authored
Merge pull request #696 from devlights:add-blank-variable-example
Add supperss-compileerror-with-blank-variable example
2 parents 7e01c0c + ca8c488 commit e0d57bd

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# これは何?
2+
3+
Goでは利用していない変数があるとコンパイルエラーとなりますが、開発途中などではその状態にしておきたいときがあります。
4+
5+
そのような場合に _ 変数に代入することでコンパイルエラーを抑止できるというサンプルです。
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# https://taskfile.dev
2+
3+
version: "3"
4+
5+
tasks:
6+
default:
7+
cmds:
8+
- task: build
9+
- task: clean
10+
build:
11+
cmds:
12+
- go build
13+
clean:
14+
cmds:
15+
- go clean
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
func main() {
4+
if err := run(); err != nil {
5+
panic(err)
6+
}
7+
}
8+
9+
func run() error {
10+
var (
11+
v any = 100
12+
)
13+
14+
// そのままにしていると v は利用されていないことになるため
15+
// コンパイルエラー (v declared and not usedcompilerUnusedVar) となる。
16+
// 一時的に抑止したい場合は _ に代入しておく
17+
_ = v
18+
19+
return nil
20+
}

0 commit comments

Comments
 (0)