英文:
How can I restrict value of variables in Golang?
问题
我知道Go语言中没有setter和getter的惯用方式。但是我需要在Go中限制变量的值。
我定义了一个新类型:
type MyNewStringType string
需要限制值的变量被定义为MyStringType类型。
MyStringType类型的变量只能有三个值:"Yes"、"No"、"I don't know"。
在Golang中,我该如何实现呢?
在Java和C++中,我可以使用setter和getter,但在Golang中不太一样。
我知道,我可以创建以下结构体:
type MyNewStringType struct {
Variable string
}
然后创建以下函数:
func (m *MyNewStringType) SetVariable(newVar string) error {
if newVar == "Yes" || newVar == "No" || newVar == "I don't know" {
m.Variable = newVar
return nil
} else {
return errors.New("Wrong value")
}
}
但我认为这种方式不太对。
英文:
I know Go idiomatic don't have setter and getter. But I need to restrict value of variables in Go.
I defined new type
type MyNewStringType string
And variables, that was defined as MyStringType, need to restrict value.
Variables of MyStringType can have only 3 values:
"Yes", "No", "I don't know"
How can I do it in Golang?
In Java, C++ I have setter and getter, but in Golang is not normal.
I know, I can create
type MyNewStringType struct {
Variable string
}
and create
func(m *MyNewStringType) SetVariable(newVar string) error {
if newVar == "Yes" || newVar == "No" || newVar == "I don't know" {
m.Variable = newVar
return nil
} else {
return errors.New("Wrong value")
}
But I think it's wrong way.
答案1
得分: 0
这个更好的实现方式可以是允许任何值,并在使用该值时进行验证。以下是一个简短的示例:
package main
import (
"fmt"
"os"
)
func main() {
fmt.Println("第一次尝试")
if err := doSomething("foo"); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
fmt.Println()
fmt.Println("第二次尝试")
if err := doSomething("Yes"); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
}
func doSomething(s string) error {
switch s {
case "Yes", "No", "I don't know":
fmt.Println("成功!")
return nil
default:
return fmt.Errorf("不支持的值:%q", s)
}
}
输出结果:
第一次尝试
不支持的值:"foo"
第二次尝试
成功!
[代码链接](https://play.golang.org/p/nt8J9wdlZkp)
如果你坚持按照你提出的方式实现,也许你的`SetVariable`方法会变得更整洁一些,就像上面的示例中使用`switch`语句一样。
<details>
<summary>英文:</summary>
A nicer way of implementing this could be to allow any value, and then validating when the value is used. A short example:
package main
import (
"fmt"
"os"
)
func main() {
fmt.Println("First try")
if err := doSomething("foo"); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
fmt.Println()
fmt.Println("Second try")
if err := doSomething("Yes"); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
}
func doSomething(s string) error {
switch s {
case "Yes", "No", "I don't know":
fmt.Println("Success!")
return nil
default:
return fmt.Errorf("unsupported value: %q", s)
}
}
Output:
First try
unsupported value: "foo"
Second try
Success!
https://play.golang.org/p/nt8J9wdlZkp
If you insist on implementing it in the way you suggest, perhaps your `SetVariable` method would become a little bit tidier if you used a `switch` statement as in the above example.
</details>
# 答案2
**得分**: -5
翻译结果:
简短回答:
type MyString string
const (
YES MyString = "yes"
NO = "no"
DONTKNOW = "i dont know"
)
func foo(arg MyString){
fmt.Print(arg)
}
foo(YES) //成功,输出"yes"
foo("jop") //编译失败
<details>
<summary>英文:</summary>
Short awnser:
type MyString string
const (
YES MyString = "yes"
NO = "no"
DONTKNOW = "i dont know"
)
func foo(arg MyString){
fmt.Print(arg)
}
foo(YES) //success, prints "yes"
foo("jop") //fails to compile
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论