How can I restrict value of variables in Golang?

huangapple go评论81阅读模式
英文:

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 (
      &quot;fmt&quot;
      &quot;os&quot;
    )

    func main() {
      fmt.Println(&quot;First try&quot;)
      if err := doSomething(&quot;foo&quot;); err != nil {
        fmt.Fprintf(os.Stderr, &quot;%v\n&quot;, err)
      }

      fmt.Println()

      fmt.Println(&quot;Second try&quot;)
      if err := doSomething(&quot;Yes&quot;); err != nil {
        fmt.Fprintf(os.Stderr, &quot;%v\n&quot;, err)
      }
    }

    func doSomething(s string) error {
      switch s {
      case &quot;Yes&quot;, &quot;No&quot;, &quot;I don&#39;t know&quot;:
        fmt.Println(&quot;Success!&quot;)
        return nil
      default:
        return fmt.Errorf(&quot;unsupported value: %q&quot;, s)
      }
    }

Output:

    First try
    unsupported value: &quot;foo&quot;
    
    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 = &quot;yes&quot;
        NO = &quot;no&quot;
        DONTKNOW = &quot;i dont know&quot;
    )

    func foo(arg MyString){
        fmt.Print(arg)
    }

    foo(YES) //success, prints &quot;yes&quot;

    foo(&quot;jop&quot;) //fails to compile

</details>



huangapple
  • 本文由 发表于 2016年11月9日 18:02:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/40504253.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定