package errors
// New returns an error that formats as the given text.
func New(text string) error {
return &errorString{text}
}
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#方法1:使用os.listdir | |
import os | |
for filename in os.listdir(r'c:\windows'): | |
print filename | |
#方法2:使用glob模块,可以设置文件过滤 | |
import glob | |
for filename in glob.glob(r'c:\windows\*.exe'): | |
print filename |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_functions = {} | |
def register(f): | |
global _functions | |
_functions[f.__name__] = f | |
return f | |
@register | |
def foo(): | |
return 'bar' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with open('test.txt') as f | |
for line in f: | |
print(line) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for init; condition; post {} | |
// 如同 c 的 while | |
for condition {} | |
// 死循环 | |
for {} | |
// 遍历数组,切片,字符串,map等 | |
for key, value := range aList {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 条件在 case 中 | |
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 只有函数声明,没有函数体,表明函数以其他语言实现 | |
func Sin(x float64) float //implemented in assembly language | |
// 可变参数 | |
func sum(values...int) int { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 闭包 trap | |
var rmdirs []func() | |
for _, d := range tempDirs() { | |
dir := d // 不能删除,否则循环删除的是同一个目录 | |
os.MkdirAll(dir, 0755) | |
rmdirs = append(rmdirs, func() { | |
os.RemoveAll(dir) | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func Parse(input string) (s *Syntax, err error) { | |
defer func() { | |
if p := recover(); p!= nil { | |
err = fmt.Errorf("internal error: %v", p) | |
} | |
}() | |
} |
Operation | A Nil Channel | A Closed Channel | A Not-Closed Non-Nil Channel |
---|---|---|---|
Close | panic | panic | succeed to close (C) |
Send Value To | block for ever | panic | block or succeed to send (B) |
Receive Value From | block for ever | never block (D) | block or succeed to receive (A) |
一个基于无缓存 channel 的发送操作将导致发送者 goroutine 阻塞, 直到另一个 goroutine 在相同的 channel 上执行接收操作, 当发送的值通过 channel 成功传输之后, 两个 goroutine 可以继续执行后面的语句, 反之,如果接收操作先发生, 那么接收者 goroutine 也将阻塞, 直到有另一个 goroutine 在相同的 channel 上执行发送操作
OlderNewer