前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Golang: Control structures

Golang: Control structures

作者头像
Miigon
发布2022-10-27 15:54:50
4710
发布2022-10-27 15:54:50
举报
文章被收录于专栏:Miigon's BlogMiigon's Blog

Control structures

Taken from Effective Go:

The control structures of Go are related to those of C but differ in important ways. There is no do or while loop, only a slightly generalized for; switch is more flexible; if and switch accept an optional initialization statement like that of for; break and continue statements take an optional label to identify what to break or continue; and there are new control structures including a type switch and a multiway communications multiplexer, select. The syntax is also slightly different: there are no parentheses and the bodies must always be brace-delimited.

If statement

In Go there’s no need for parenthesis in If statements. Open curly braces must be on the same line with the if keyword. (due to Go’s “semicolon insertion” feature)

代码语言:javascript
复制
if x> > 0 {
	return y
}
代码语言:javascript
复制
if err := myPack.SomeOperation(); err != nil {
	log.Print(err)
	return err
}

Switch statement

Writing if-else chain as a switch

Go’s switch is more general than C’s. It’s possible (and idiomatic) to write a if-else-if-else chain as a switch.

代码语言:javascript
复制
func unhex(c byte) byte {
	switch {
	case '0' <= c && c <= '9':
		return c - '0'
	case 'a' <= c && c <= 'f':
		return c - 'a' + 10
	case 'A' <= c && c <= 'F':
		return c - 'A' + 10
	}
	// notice how there is no `break`. In Go a case wouldn't fall through
	// the next case like C would, even without `break`
	return 0
}

No automatic fall-through

Like we said in the above code, Go does not have automatic fall-through. This means we don’t have to write break for each and every case.

But what if we actually needed multiple case to land on the same piece of code? Fortunately, Go provides us with ‘comma-separated lists’.

Comma-separated lists

代码语言:javascript
复制
func shouldEscape(c byte) bool {
	switch c {
	case ' ', '?', '&', '=', '#', '+', '%':
		return true
	}
	return false
}

This is the equivalence of the following C code:

代码语言:javascript
复制
bool shouldEscape(char c) {
	switch(c) {
		case ' ':
		case '?':
		case '&':
		case '=':
		case '#':
		case '+':
		case '%':
			return true;
		default:
			return false;
	}
}

Type switch

// Working in Progress…

‘break’ with label

// Working in Progress…

Defer statement

Go’s defer statement schedules a function call (the deferred function) to be run immediately before the function executing the defer returns. It’s an unusual but effective way to deal with situations such as resources that must be released regardless of which path a function takes to return.

This feature is quite unique and C++ doesn’t have anything like it. For this reason, we don’t really have anything to compare it against. For more advanced usage of the statement definitely checkout Effective Go#Defer

In this example, f will always be closed wherever we return in the function as long as it’s after the defer f.Close() statement.

代码语言:javascript
复制
func Contents(filename string) (string, error) {
	f, err := os.Open(filename)
	if err != nil {
		return "", err			// before defer statement, so wouldn't trigger `f.Close()`
	}
	defer f.Close()				// f.Close() will run when we're finished.
	
	// do something...

	if /* ... */ {
		return nil, "error!"	// f will be closed if we return here.
	}

	return string(result), nil	// f will be closed if we return here.
}

Taken from Effective Go:

For programmers accustomed to block-level resource management from other languages, defer may seem peculiar, but its most interesting and powerful applications come precisely from the fact that it’s not block-based but function-based.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Control structures
    • If statement
      • Switch statement
        • Writing if-else chain as a switch
        • No automatic fall-through
        • Comma-separated lists
        • Type switch
      • ‘break’ with label
        • Defer statement
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档