当前位置:网站首页>Syntax error of go language generic in IDE

Syntax error of go language generic in IDE

2022-06-26 04:04:00 Hand grinding procedure

Problem description

Use goland Use generics ,go Version is :
go version go1.18.1 windows/amd64
Write code :

func Test[T any](a, b T) T {
	return a
}

func Add[T string | int | int64 | float64](a, b T) T {
	return a + b
}

func main() {
	fmt.Println("hello")
	res := Test(1.1, 2.2)
	fmt.Println(res)
}

There are the following problems :
#command-line-arguments
.\demo02.go:5:17: syntax error: unexpected type, expecting ]

resolvent

This is because goland Generic format not supported : Some versions can be extended with external tools :
The official website tutorial is as follows
https://blog.jetbrains.com/go/2020/11/24/experimenting-with-go-type-parameters-generics-in-goland/
It is recommended to update directly to 2022 Later versions

原网站

版权声明
本文为[Hand grinding procedure]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260356339090.html