no-op/explicitly do nothing in go

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

no-op/explicitly do nothing in go

问题

我有一个类型方法,可以改变类型的字段。它不接受任何参数,也不返回任何值。该方法的主要部分是一个switch块。我想要能够在switch块中使用一个空操作来“短路”退出。在将其重构为类型方法之前,我可能会直接从函数中返回,但那样不行。删除该case将破坏方法的逻辑——default情况会改变状态,如果匹配到该情况,我不想这样做。我需要类似于Python中的pass的功能。

代码:

  1. func (parser *Parser) endSectionName() {
  2. state := parser.State
  3. buffer := parser.buffer
  4. results := parser.results
  5. switch {
  6. case state.HasFlag(IN_ESCAPED):
  7. // ???
  8. case !inSection(state):
  9. return state, NotInSectionError
  10. case !state.HasFlag(IN_SECTION_NAME):
  11. state.Reset()
  12. return state, errors.New("Parsing error: Not in section name")
  13. default:
  14. state.RemoveFlag(IN_SECTION_NAME)
  15. s := buffer.String()
  16. results[s] = new(Section)
  17. buffer.Reset()
  18. return state, nil
  19. }
  20. }

请注意,我只翻译了代码部分,其他内容不包括在内。

英文:

I have a type method that mutates the type's fields. It takes no arguments and returns nothing. The bulk of the method is a switch block. I want to be able to "short-circuit" out of the switch block with a no-op. Before I refactored it into a type method, I would've just returned out of the function, but that's out. Removing the case would break the logic of the method--the default case mutates state, which I don't want to do if this case is matched. I need the equivalent of Python's pass, basically.

Code:

  1. func (parser *Parser) endSectionName () {
  2. state = parser.State
  3. buffer = parser.buffer
  4. results = parser.results
  5. switch {
  6. case state.HasFlag(IN_ESCAPED) {
  7. // ???
  8. }
  9. case !inSection(state) {
  10. return state, NotInSectionError
  11. }
  12. case !state.HasFlag(IN_SECTION_NAME) {
  13. state.Reset()
  14. return state, errors.New("Parsing error: Not in section name")
  15. }
  16. default {
  17. state.RemoveFlag(IN_SECTION_NAME)
  18. s := buffer.String()
  19. results
    展开收缩
    = new(Section)
  20. buffer.Reset()
  21. return state, nil
  22. }
  23. }
  24. }

答案1

得分: 31

与其他语言不同,在Go语言中,控制流在每个case语句处中断,除非使用fallthrough语句明确要求控制流进入下一个case

而且,在case之后不需要必须有语句(可以为空)。看下面的例子:

  1. i := 3
  2. switch i {
  3. case 3:
  4. case 0:
  5. fmt.Println("Hello, playground")
  6. }

即使i==3并且在case 3之后没有语句,它也不会打印任何内容。

与此类似的是:

  1. i := 3
  2. switch {
  3. case i == 3:
  4. case i == 0:
  5. fmt.Println("Hello, playground")
  6. }

Go Playground上尝试一下。

英文:

Unlike in other languages, in Go the control flow breaks at each case of a switch statement, control doesn't flow into the next case unless it is explicitly "asked" for with the fallthrough statement.

And also a statement is not required after the case (it can be empty). See this example:

  1. i := 3
  2. switch i {
  3. case 3:
  4. case 0:
  5. fmt.Println("Hello, playground")
  6. }

It will print nothing even though i==3 and there is no statement after case 3.

Same as this:

  1. i := 3
  2. switch {
  3. case i == 3:
  4. case i == 0:
  5. fmt.Println("Hello, playground")
  6. }

Try it on the Go Playground.

huangapple
  • 本文由 发表于 2015年4月21日 22:56:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/29775836.html
匿名

发表评论

匿名网友

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

确定