0
0

don't use underscores in Go names;の簡易的な対処法(初学者向け)

Last updated at Posted at 2020-12-01

はじめに

Goを最近勉強していて変数宣言で"don't use underscores in Go names"が出たのでメモ書き。

事象

bool型の変数を定義して、結果と型を出力させるという簡単なコードです。

package main

import (
	"fmt"
	"reflect"
)

func main() {
	a := 1
	b := 5
	var num_bool bool = a > b //ここに "don't use underscores in Go names;"が出てる

	fmt.Println(num_bool)
	fmt.Println(reflect.TypeOf(num_bool))
}

そのままでも以下のようにビルドは通りますが何か気持ちが悪いので対処します。

PS C:\pg\Go\study> go build main.go
PS C:\pg\Go\study> ./main
false
bool

対処

don't use underscores in Go names;
雑に和訳すると、"名前にアンダーバー入れないでね"って言われてますね、
つまり、num_boolnumBool にしてやればいいですね。

package main

import (
	"fmt"
	"reflect"
)

func main() {
	a := 1
	b := 5
	var numBool bool = a > b

	fmt.Println(numBool)
	fmt.Println(reflect.TypeOf(numBool))
}

以上

最後に

変数の命名はスネークケースって手癖になってたので、この辺は言語ごとに慣れていきたいなーと感じたこの頃。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0