3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

golang: no new variables on left side of := が出力

Posted at

goを最近やり始めました。ハマった事項をめもめも。

エラーになったコード

fmt.println("AAAAA")
_, err := fmt.Scanln(&val1)
fmt.println("BBBBB")
_, err := fmt.Scanln(&val2)
fmt.println("CCCCC")
_, err := fmt.Scanln(&val3)

2回目の_, err := fmt.Scanln(&val2)no new variables on left side of :=

fmt.Scanlnは、
func Scanf(format string, a ...interface{}) (n int, err os.Error)
でリターンがあるが、最初の"n"は使わない。
なので、"_"にすると、このエラー。

同じ変数を繰り返し上書きする、手抜きコードはダメなんですね。

修正したコード

	if asktype == "s" {
		fmt.Print("Server Name? ")

	} else if asktype == "p" {
		fmt.Print("Port Number? ")

	} else {
		fmt.Print("Token? ")

	}
	_, err := fmt.Scanln(&value)
	if err != nil {
		log.Fatal(err)
	}
	return value

エラーを1箇所に集約すれば、できた。
という解釈であっているのか?

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?