Goで数値を文字列にする方法
strconvで変換する。
import ( "fmt" "strconv" ) func subtractProductAndSum(n int) int { sum, prod := 0, 1 for _, x := range fmt.Sprintf("%d", n) { xi, _ := strconv.Atoi(string(x)) sum += xi prod *= xi } return prod - sum }
strconvで変換する。
import ( "fmt" "strconv" ) func subtractProductAndSum(n int) int { sum, prod := 0, 1 for _, x := range fmt.Sprintf("%d", n) { xi, _ := strconv.Atoi(string(x)) sum += xi prod *= xi } return prod - sum }