
Golang
文章平均质量分 50
会飞的bird
这个作者很懒,什么都没留下…
展开
-
Golang 匿名函数、闭包
参考:https://blog.youkuaiyun.com/qq_35976351/article/details/81986496Golang 闭包匿名函数Golang支持匿名函数,即在需要使用函数时,再定义函数,匿名函数没有函数名,只有函数体。匿名函数经常被用于实现回调函数、闭包等。匿名函数可以动态的创建,普通函数必须在包中编译前就定义完毕,而匿名函数可以随时改变功能。func main() { f := func(i int) { fmt.Println(i) } f(10) f =.原创 2022-02-06 14:02:56 · 440 阅读 · 0 评论 -
Golang中的error
文章目录Golang中的errorerror源码error创建Unwrap()、Is() 和 As()Golang中的errorerror源码type error interface { Error() string}error 是一个接口类型,它包含一个 Error() 方法,返回值为 string。任何实现这个接口的类型都可以作为一个错误使用,Error()这个方法提供了对错误的描述。error创建errors.New()// 源码如下:package errorsfunc原创 2022-01-05 13:24:23 · 817 阅读 · 0 评论 -
Go 排序方式
参考:https://segmentfault.com/a/1190000016514382 (值得一看)Go 常见排序方式整数、浮点数、字符串切片排序sort包提供了一下几种排序函数:sort.Ints(x []int)sort.Float64s(x []float64)sort.Strings(x []string)使用自定义比较器进行排序使用sort.Slice(x interfacec{}, less func(i, j int) bool)进行排序(快速排序 + 堆排.原创 2021-10-30 13:54:39 · 555 阅读 · 0 评论 -
Golang实现最大堆/最小堆
Golang实现最大堆/最小堆参考: https://yangjiahao106.github.io/2019/01/15/golang-%E6%9C%80%E5%A4%A7%E5%A0%86%E5%92%8C%E6%9C%80%E5%B0%8F%E5%A0%86/https://studygolang.com/articles/24288方法一此方法就是比较传统、常见的方法,下面来构建一个最小堆:type Heap struct { Size int Elems []int}fun原创 2021-10-17 12:18:31 · 2383 阅读 · 0 评论 -
http.ListenAndServe()到底做了什么?
参考:https://studygolang.com/articles/25849?fr=sidebar http://blog.youkuaiyun.com/gophers实现一个最简短的hello world服务器package mainimport "net/http"func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(`hello wor.原创 2021-09-10 16:03:21 · 1233 阅读 · 0 评论 -
Slice的本质
Slice的本质我们先看下面的代码,看看它的输出是什么:package mainimport "fmt"type Slice []intfunc (A Slice) Append(value int) { A = append(A, value)}func main() { mSlice := make(Slice, 10, 20) mSlice.Append(5) fmt.Println(mSlice)}//output: [0 0 0 0 0 0 0 0 0 0]我们原创 2021-08-26 10:52:19 · 110 阅读 · 0 评论 -
Go unsafe Pointer
Go unsafe PointerGo被设计为一种强类型的静态语言,强类型意味着类型一旦确定就无法更改,静态意味着类型检查在运行前就做了。指针类型转换为了安全考虑,两个不同类型的指针不能相互转换,例如:package mainfunc main() { i := 10 ip := &i var fp *float64 = (*float64)(ip) //会提示 Cannot convert an expression of the type '*int' to the原创 2021-08-26 10:12:16 · 782 阅读 · 0 评论 -
Go中new和make的区别
Go中new和make的区别变量声明当我们声明变量时可以使用var关键字,当不指定变量的默认值时,这些变量的默认值就是他们的零值,比如int的默认值为0,string的默认值为"",引用类型的零值为nil。但是当我们在声明引用类型的变量并直接使用时,会panic。package mainimport "fmt"func main() { var i *int *i = 10 fmt.Println(*i)}//output: panic: runtime error: inval原创 2021-08-26 10:10:51 · 250 阅读 · 0 评论