Skip to content

Instantly share code, notes, and snippets.

View xin053's full-sized avatar
🎯
Focusing

xin053 xin053

🎯
Focusing
View GitHub Profile
@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 / 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 / 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 / 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 / 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 / select.go
Created July 5, 2019 06:35
[go select] go select #go #select
select {
case <-ch1:
//...
case x:= <-ch2:
//...
case ch3 <- y:
//...
default:
//...
}
@xin053
xin053 / crypto_rand.go
Last active July 5, 2019 07:49
[go random]go random #go #random
// 更安全的随机
package main
import (
"crypto/rand"
// "encoding/base64"
// "encoding/hex"
"fmt"
)
@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 / interface.go
Last active July 5, 2019 09:41
[go interface{}] go interface{} #go #interface
// 故意在接口中定义方法, 以防止其他类型无意中实现了该接口
type runtime.Error interface {
error
RuntimeError()
}
// or
type testing.TB interface {
Error(args ...interface{})
Errorf(format string, args ...interface{})