cannot assign to x (neither addressable nor解决方法

cannot assign to x (neither addressable

Golang程序代码中,IDE提示:cannot assign to x (neither addressable nor a map index expression),即:无法分配给x(既不是可寻址的,也不是映射索引表达式),就像下面的这段代码:

package main

import "fmt"

func main() {
	const y float32 = 0.5
	y = 0.35
	fmt.Println(y)

}

原因

如果是类似上面这段代码而出现的cannot assign to x (neither addressable nor a map index expression),那么原因就是:代码在修改常量,而go语言中的常量是不可修改的。

解决方法

根据不同的需求,有不同的解决方法,如下:

  • 如果确实是需要常量,那么就将修改常量的代码删除掉;
  • 如果确实是需要修改某一个变量的值,那么就不要将此使用const声明为常量,即把const关键字删除掉即可;

免责声明:内容仅供参考,不保证正确性。


全栈后端 / go语法 :













Copyright © 2022-2024 笨鸟工具 x1y1z1.com All Rights Reserved.