【已解决】Go:cannot use nums (type []int) as type string in argument to fmt.Printf

在刚学习Go语言的时候,遇到这个问题一直不能成功输出,最后找到了问题所在。

问题描述:

package main
import "fmt"
func main() {
    nums := []int{}
	nums = append(nums, 1)
	fmt.Printf(nums)
}

输出结果为:

# command-line-arguments
./main.go:6:12: cannot use nums (type []int) as type string in argument to fmt.Printf

解决办法:

第一种:将fmt.Printf改成fmt.Println

第二种:将fmt.Printf(nums)改成fmt.Printf("%v", nums)

package main
import "fmt"
func main() {
    nums := []int{}
	nums = append(nums, 1)
	fmt.Println(nums)
}


//或者

package main
import "fmt"
func main() {
    nums := []int{}
	nums = append(nums, 1)
	fmt.Printf("%v",nums)
}

猜你喜欢

转载自blog.csdn.net/qq_43681154/article/details/127391204