Skip to content

Instantly share code, notes, and snippets.

View xin053's full-sized avatar
🎯
Focusing

xin053 xin053

🎯
Focusing
View GitHub Profile
@xin053
xin053 / list_all_file.py
Last active July 4, 2019 07:50
[python 遍历文件]遍历目录下所有文件 #python #文件
#方法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
@xin053
xin053 / register_decorator.py
Last active July 4, 2019 07:49
[python 装饰器]注册装饰器 #python #装饰器
_functions = {}
def register(f):
global _functions
_functions[f.__name__] = f
return f
@register
def foo():
return 'bar'
@xin053
xin053 / read_file.py
Last active July 4, 2019 07:47 — forked from y1zhou/read_n_lines.py
[python 读取文件]读文件一次读n行 #python #文件
with open('test.txt') as f
for line in f:
print(line)
@xin053
xin053 / for.go
Last active July 5, 2019 07:51
[go for循环] for 循环 #go #for
for init; condition; post {}
// 如同 c 的 while
for condition {}
// 死循环
for {}
// 遍历数组,切片,字符串,map等
for key, value := range aList {}
@xin053
xin053 / switch.go
Created July 4, 2019 08:16
[go switch] go switch #go #switch
// 条件在 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
}
@xin053
xin053 / func.go
Created July 4, 2019 16:39
[go 函数]go 函数 #go #函数
// 只有函数声明,没有函数体,表明函数以其他语言实现
func Sin(x float64) float //implemented in assembly language
// 可变参数
func sum(values...int) int {
}
@xin053
xin053 / trap.go
Last active February 5, 2021 02:45
[go 陷阱]go 陷阱 #go #陷阱
// 闭包 trap
var rmdirs []func()
for _, d := range tempDirs() {
dir := d // 不能删除,否则循环删除的是同一个目录
os.MkdirAll(dir, 0755)
rmdirs = append(rmdirs, func() {
os.RemoveAll(dir)
})
}
@xin053
xin053 / recover.go
Last active July 11, 2019 06:54
[go recover] go recover #go #recover
func Parse(input string) (s *Syntax, err error) {
defer func() {
if p := recover(); p!= nil {
err = fmt.Errorf("internal error: %v", p)
}
}()
}
@xin053
xin053 / errors.md
Created July 5, 2019 02:03
[go errors]go errors #go #errors

errors.go 源码

package errors

// New returns an error that formats as the given text.
func New(text string) error {
	return &errorString{text}
}
@xin053
xin053 / channel.md
Last active April 4, 2020 12:34
[go channel] go channel #go #channel

channel 重点

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 上执行发送操作