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
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
_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
#方法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
// 条件在 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
select { | |
case <-ch1: | |
//... | |
case x:= <-ch2: | |
//... | |
case ch3 <- y: | |
//... | |
default: | |
//... | |
} |
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
// 更安全的随机 | |
package main | |
import ( | |
"crypto/rand" | |
// "encoding/base64" | |
// "encoding/hex" | |
"fmt" | |
) |
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
// 故意在接口中定义方法, 以防止其他类型无意中实现了该接口 | |
type runtime.Error interface { | |
error | |
RuntimeError() | |
} | |
// or | |
type testing.TB interface { | |
Error(args ...interface{}) | |
Errorf(format string, args ...interface{}) |
OlderNewer