go
VVVinegar
这个作者很懒,什么都没留下…
展开
-
go-正则
import ( "fmt" "regexp")const text = `my email is ccmouse@gmail.com@abc.comemail1 is abc@def.orgemail2 is kkk@qq.comemail3 is ddd@abc.com.cn`func main() { re := regexp.MustCompile( ...原创 2019-12-05 22:16:23 · 232 阅读 · 0 评论 -
go-http
1.使用http客户端发送请求2.使用http.Client控制请求头部等3.使用httputil简化工作package mainimport ( "fmt" "net/http" "net/http/httputil")func main() { request, err := http.NewRequest( http.MethodGet, "http:...原创 2019-12-02 21:47:36 · 369 阅读 · 0 评论 -
go-迷宫算法
maze.in文件:6 50 1 0 0 00 0 0 1 00 1 0 1 01 1 1 0 00 1 0 0 10 1 0 0 0maze.go文件:package mainimport ( "fmt" "os")func main() { maze := readMaze("maze/maze.in") step := walk(maze, ...原创 2019-12-02 10:30:03 · 312 阅读 · 0 评论 -
go-select
1.select的使用2.定时器的使用3.在select中使用nil channel,实现channel中有值才处理,避免处理空值func generator() chan int { out := make(chan int) go func() { i := 0 for { time.Sleep( time.Duration(rand.Intn(1500...原创 2019-11-28 22:06:28 · 154 阅读 · 0 评论 -
go-用channel进行树的遍历
func (node *Node) Traverse() { node.TraverseFunc(func(n *Node) { n.Print() }) fmt.Println()}func (node *Node) TraverseFunc(f func(*Node)) { if node == nil { return } node.Left.TraverseF...原创 2019-11-27 21:43:40 · 348 阅读 · 0 评论 -
go-使用channel等待任务结束
1.使用channel等到任务结束func doWork(id int, c chan int, done chan bool) { for n := range c { fmt.Printf("Worker %d received %c\n", id, n) go func() {done <- true}() }}type worker struct { ...原创 2019-11-26 22:18:36 · 250 阅读 · 0 评论 -
go-channel
1.channel2.buffered channel3.range关键字接收channel中的内容func worker(id int, c chan int) { for n := range c { fmt.Printf("Worker %d received %c\n", id, n) }}func createWorker(id int) chan&l...原创 2019-11-26 22:07:08 · 31221 阅读 · 0 评论 -
go-http测试
errwrapper_test.go:import ( "errors" "fmt" "io/ioutil" "net/http" "net/http/httptest" "os" "strings" "testing")func errPanic(_ http.ResponseWriter, _ *http.Request) error { panic(123)...原创 2019-11-24 18:32:02 · 473 阅读 · 0 评论 -
go-性能调优手段
1.使用命令行生成性能数据文件->go test -bench . -cpuprofile cpu.out2.查看性能数据文件->go tool pprof cpu.out ->web // web命令需要安装可视化工具:http://www.graphviz.org/download/...原创 2019-11-20 21:49:48 · 196 阅读 · 0 评论 -
go-表格驱动测试
求最长不重复字串方法如下:func lengthOfNonRepeatingSubStr(s string) int { lastOccurred := make(map[rune]int) start := 0 maxLength := 0 for i, ch := range []rune(s) { if lastI, ok := lastOccurred[ch]; ok ...原创 2019-11-18 22:09:25 · 263 阅读 · 0 评论 -
go-错误处理
摘要:实现一个读取文件内容的服务。通过defer+panic+recover及type assertion实现错误处理。handler.go(实现读取):package filelistingimport ( "io/ioutil" "net/http" "os" "strings")const prefix = "/list/"type userEr...原创 2019-11-13 22:08:35 · 185 阅读 · 0 评论 -
go-函数实现接口
type intGen func() intfunc fibonacci() intGen { a, b := 0, 1 return func() int { a, b = b, a+b return a }}func (g intGen) Read( p []byte) (n int, err error) { next := g() if next > ...原创 2019-11-10 15:17:51 · 370 阅读 · 0 评论 -
go-字符串
func main() { s := "Yes我爱慕课网!" // UTF-8 fmt.Println(s) for _, b := range []byte(s) { fmt.Printf("%X ", b) } fmt.Println() for i, ch := range s { // ch is a rune fmt.Printf("(%d %X) ", i, ...原创 2019-10-24 21:54:56 · 150 阅读 · 0 评论 -
go-map
func main() { m := map[string]string{ "name": "ccmouse", "course": "golang", "site": "imooc", "quality": "notbad", } m2 := make(map[string]int) // m2 == empty map var m3 map[stri...原创 2019-10-24 21:31:10 · 147 阅读 · 0 评论 -
go-切片操作
func printSlice(s []int) { fmt.Printf("%v, len=%d, cap=%d\n", s, len(s), cap(s))}func main() { fmt.Println("Creating slice") var s []int // Zero value for slice is nil for i := 0; i < 10...原创 2019-10-23 22:57:39 · 176 阅读 · 0 评论 -
go-切片概念
func updateSlice(s []int) { s[0] = 100}func main() { arr := [...]int{0, 1, 2, 3, 4, 5, 6, 7} fmt.Println("arr[2:6] =", arr[2:6]) fmt.Println("arr[:6] =", arr[:6]) s1 := arr[2:] fmt.Println(...原创 2019-10-23 22:51:05 · 147 阅读 · 0 评论 -
go-常量和枚举
const( // go 中枚举类型用常量表示 cpp = iota // iota表示自增 _ // _表示跳过 python golang java)println(cpp, python, golang, java)打印结果:0 2 3 4const( ...原创 2019-09-26 20:58:08 · 203 阅读 · 0 评论 -
go-条件语句
go的if语句可以这么用:import ( "fmt" "io/ioutil")const filename = "abc.txt"if contents, err := ioutil.ReadFile(filename); err != nil { // contents作用域仅在该block下 fmt.Println(err)} else { fmt.Print...原创 2019-09-26 21:22:08 · 122 阅读 · 0 评论 -
go-循环
for语句:// 十进制转二进制func convertToBin(n int) string { result := "" for ; n > 0; n /= 2 { lsb := n % 2 result = strconv.Itoa(lsb) + result } return result}func forever() { for { // 相当...原创 2019-09-26 21:40:51 · 142 阅读 · 0 评论 -
go-文件读取
// 一行一行读取func printFile1(filename string) { file, err := os.Open(filename) if err != nil { panic(err) } printFileContents(file)}func printFileContents(reader io.Reader) { scanner := bufio...原创 2019-09-26 21:44:04 · 207 阅读 · 0 评论 -
go-内建变量
bool, string(u)int, (u)int8, (u)int16, (u)int32, (u)int64(u)uintptr // 指针byte, rune // rune,32位4字节,go的字符类型float32, float64complex64, complex128 // 复数,欧拉公式:cmplx.Exp(1i*math.P...原创 2019-09-25 22:32:23 · 193 阅读 · 0 评论