1
1

More than 3 years have passed since last update.

multiple-value in single-value context

Posted at
main.go
package main

import "fmt"
import "strconv"

func main() {
    str := "12"
    num, _ := strconv.Atoi(str) + 1 
    fmt.Println(num)
}

本日のエラー

multiple-value strconv.Atoi() in single-value context
(一つの値が来るべきコンテキストに複数の値があるよ)

strconv.Atoi()は複数の戻り値を返すので、その実行結果に同文内で直接演算しようとしても当然できない。
戻り値を複数返せない言語を主に扱ってきた人は案外引っかかってしまうかも。(はい、私です。)

対処

num, _ := strconv.Atoi(str)
num++

ちなみに

num := getNum() + 1

func getNum() int {
    return 12
}

これはgetNum()が一つの戻り値しか返さないので問題ない。

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