随笔分类 -  go

摘要:不带缓存区的, 必需要goroutine等待着从队列中取,才能放入无缓存的队列中 package main import ( "fmt" "time" ) func produce(c chan int) { fmt.Println("produce start") time.Sleep(time. 阅读全文
posted @ 2022-03-10 21:50 ty1539 阅读(36) 评论(0) 推荐(0) 编辑
摘要:用死循环的方式从ch中获取数据, 但是得自己判断ch是否已经取空了 点击查看代码 package main import ( "fmt" ) func producer(chnl chan int) { for i := 0; i < 10; i++ { chnl <- i } defer clos 阅读全文
posted @ 2022-03-10 21:49 ty1539 阅读(14) 评论(0) 推荐(0) 编辑
摘要:点击查看代码 package main import ( "fmt" "time" ) func producer(chnl chan int) { for i := 0; i < 10; i++ { chnl <- i time.Sleep(time.Second) } close(chnl) } 阅读全文
posted @ 2022-03-10 21:42 ty1539 阅读(18) 评论(0) 推荐(0) 编辑
摘要:点击查看代码 package main import ( "fmt" "time" ) func write(ch chan int) { for i := 0; i < 5; i++ { ch <- i fmt.Println("successfully wrote", i, "to ch") } 阅读全文
posted @ 2022-03-10 21:33 ty1539 阅读(13) 评论(0) 推荐(0) 编辑
摘要:利用goroutine等待的取出元素的时机,没有元素取出来,就一直阻塞,等待goroutine协程完成,取出队列中的元素,该进程才结束 点击查看代码 package main import ( "fmt" "time" ) func hello(c chan bool) { time.Sleep(5 阅读全文
posted @ 2022-03-10 21:14 ty1539 阅读(28) 评论(0) 推荐(0) 编辑
摘要:点击查看代码 package main import ( "fmt" "time" ) func numbers() { for i := 1; i <= 5; i++ { time.Sleep(250 * time.Millisecond) fmt.Printf("%d ", i) } } fun 阅读全文
posted @ 2022-03-10 21:01 ty1539 阅读(20) 评论(0) 推荐(0) 编辑
摘要:https://blog.csdn.net/skh2015java/article/details/83583516 点击查看代码 //http请求 func httpHandle(method, urlVal,data string) { client := &http.Client{} var 阅读全文
posted @ 2022-03-07 23:56 ty1539 阅读(1576) 评论(0) 推荐(0) 编辑
摘要:https://blog.csdn.net/qq_21852449/article/details/88093941 点击查看代码 package hsession import ( "crypto/rand" "encoding/base64" "io" "net/http" "net/url" 阅读全文
posted @ 2022-03-07 23:47 ty1539 阅读(413) 评论(0) 推荐(0) 编辑
摘要:点击查看代码 package main import "fmt" type Animal interface{ Talk() Eat() Name() string } type Dog struct{} func (d *Dog) Talk(){ fmt.Println("汪汪汪") } func 阅读全文
posted @ 2022-03-07 23:44 ty1539 阅读(37) 评论(0) 推荐(0) 编辑
摘要:点击查看代码 package main import "fmt" type Animal interface{ Talk() Eat() Name() string } type Dog struct{} func (d Dog) Talk(){ fmt.Println("汪汪汪") } func 阅读全文
posted @ 2022-03-07 23:09 ty1539 阅读(22) 评论(0) 推荐(0) 编辑
摘要:https://github.com/markusleevip/go-shici https://github.com/joizhang/learn-golang 阅读全文
posted @ 2022-03-07 21:41 ty1539 阅读(37) 评论(0) 推荐(0) 编辑
摘要:package main import ( "fmt" ) type User struct{ Username string Sex string Age int AvatarUrl string } func initUser1() { var user *User fmt.Printf("us 阅读全文
posted @ 2022-03-06 23:36 ty1539 阅读(156) 评论(0) 推荐(0) 编辑
摘要:json 序列化 package main import ( "encoding/json" "fmt" ) type User struct { UserName string `json:"姓名"` // 有了tag,序列化出来的就是tag中的名字 Sex string `json:"性别"` 阅读全文
posted @ 2022-03-06 23:27 ty1539 阅读(277) 评论(0) 推荐(0) 编辑
摘要:点击查看代码 package main import( "fmt" ) func testIf(){ var num int =2 if num == 1{ fmt.Printf("num:%d num==1 \n",num) }else if num == 2{ fmt.Printf("num:% 阅读全文
posted @ 2022-03-06 23:17 ty1539 阅读(22) 评论(0) 推荐(0) 编辑
摘要:点击查看代码 package main import ( "fmt" ) func testFor1() { var i int for i = 1; i <= 10; i++ { fmt.Printf("i=%d\n", i) } fmt.Printf("最终的i=%d\n", i) } func 阅读全文
posted @ 2022-03-06 23:12 ty1539 阅读(27) 评论(0) 推荐(0) 编辑
摘要:点击查看代码 package main import "fmt" // 选择排序 func insert_sort(a [8]int) [8]int { for i := 1; i<len(a); i++{ for j := i; j>0; j--{ if a[j] < a[j-1] { a[j], 阅读全文
posted @ 2022-03-06 22:57 ty1539 阅读(20) 评论(0) 推荐(0) 编辑
摘要:点击查看代码 package main import ( "fmt" "math" ) func justify(n int) bool { sqrtN := int(math.Sqrt(float64(n))) if n <= 1 { return false } for i := 2; i < 阅读全文
posted @ 2022-03-06 21:46 ty1539 阅读(34) 评论(0) 推荐(0) 编辑
摘要:package main import ( "fmt" ) func rangeStr() { var str string = "asdf 7461 ASDX 我打扫" utfChars := []rune(str) fmt.Println("str>>",str,len(str),"utfCha 阅读全文
posted @ 2022-03-06 21:29 ty1539 阅读(68) 评论(0) 推荐(0) 编辑
摘要:注意:testdefer3() defer 与testdefer4() defer匿名函数 的区别,这是易错点 package main import ( "fmt" ) // defer在函数返回的时候执行,多用于资源释放(打开文件,打开数据库连接,函数返回之后,关闭连接) func testde 阅读全文
posted @ 2022-03-06 19:24 ty1539 阅读(97) 评论(0) 推荐(0) 编辑
摘要:注意,先声明变量之后,通过for循环累加, for循环之后变量的值,跟着变化 点击查看代码 package main import "fmt" var a int =100 func testLocalVariable(){ var b int = 50 var a int = 501 //同时存在 阅读全文
posted @ 2022-03-06 19:14 ty1539 阅读(111) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示