如何设置限制或只是注意函数参数的提示

김종현*_*김종현 -2 types arguments function go

如何设置参数限制如下?

// 1.

func ChooseColor(color string:"black|white" ) {
  fmt.Println(color)
}

ChooseColor("white") // console "white"
ChooseColor("yellow") // console panic
Run Code Online (Sandbox Code Playgroud)

如果你觉得菜鸟不能理解上面的解决方案,那么交替看下面

// 2.

/**
* arg: "black|white"
*
*/
func ChooseColor(color string) {
  fmt.Println(color)
}

ChooseColor(  ) // IDE can notice "color: black || white"
Run Code Online (Sandbox Code Playgroud)

请帮帮我(TT)

小智 5

您可以创建您的类型Color并拥有此类型的常量。像这样的东西

type Color string

const (
    ColorBlack Color = "black"
    ColorWhite Color = "white"
)

func ChooseColor(color Color) {
    fmt.Println(color)
}
Run Code Online (Sandbox Code Playgroud)