golang 常见疑惑总结
经常会有一些朋友问go语言的一些问题和疑惑,其实好多问题在官方文档和stackoverflow里都有详细的讲解,只要你肯花时间读一遍官方文档和Effective Go基本上都有找到答案。本文总结一下大家经常问到的一些问题,长期更新。
代码都在github上, 地址 https://github.com/lpxxn/gocommonquestions
new 和make 的区别
简单来说,new(T)用于分配内存,返回指向T类型的一个指针,指针的值为T类型的零值
n1 := new(int)
fmt.Println(n1) // console the address of n1
fmt.Println(*n1 == ) // zero value type T struct {
I int
A string
Next *T
}
n2 := new(T)
fmt.Println(n2)
fmt.Println(n2.I == )
fmt.Println(n2.Next == nil)
fmt.Println(n2.A == "") n3 := new([]int)
fmt.Println(n3)
fmt.Println(*n3 == nil)
make(T) 只能用于slice、map和channel, 返回非零值的T。
m1 := make([]int, )
fmt.Println(m1)
m2 := make(map[int]string)
m2[] = "abcde"
m3 := make(chan int) m4 := make(chan int, )
make 返回的是类型本身,new 返回的是指向类型的指针
相关讲解
https://stackoverflow.com/questions/9320862/why-would-i-make-or-new
https://golang.org/doc/effective_go.html#allocation_new
https://golang.org/ref/spec#The_zero_value
是否需要主动关闭channel
除非你的程序需要等待channel关闭后做一些操作,不用主动去关闭channel。当没有地方在使用这个channel的时候go的垃圾回收系统会自动回收,如果channel里还有值,但是没有地方引用了,也会被回收。
下面的小例子就是在等待channel c1和c2关闭后,再做一些事情。
func main() {
c1 := make(chan int, )
go test1(c1)
for v := range c1 {
fmt.Println(v)
} fmt.Println("after close c1 do something")
c2 := make(chan bool)
go func() {
time.AfterFunc(time.Second * , func() {
close(c2)
})
}()
_, close := <- c2
if !close {
fmt.Println("after c2 closed do something")
} fmt.Println("end")
} func test1(c chan<- int) {
for i := ; i < ; i++ {
c <- i
}
close(c)
}
简单测试垃圾回收channel
func createChan() chan int64{
c := make(chan int64, )
c <-
return c
} func main() { RM()
for i := ; i < ; i++ {
c := createChan()
c <-
RM()
runtime.GC()
}
RM()
} func RM(){
var m runtime.MemStats
runtime.ReadMemStats(&m)
fmt.Println(m.Alloc)
}
相关讲解:https://stackoverflow.com/questions/8593645/is-it-ok-to-leave-a-channel-open
https://groups.google.com/forum/#!msg/golang-nuts/pZwdYRGxCIk/qpbHxRRPJdUJ
https://groups.google.com/forum/#!topic/golang-nuts/KtIyc5lTRJY
Unbuffered channel和buffered channel 区别
buffered channel
c3 := make(chan bool, ) // buffered channel
buffered channel 可以持续的发送数据到channel,直到channel满为至。不用等待是否有接收channel。
如果channel满了,会等待读取channel,当有channel被读取,就会继续发送数据到channel
c3 := make(chan bool, ) // buffered channel go func() {
for i := ; i < ; i++ {
c3 <- i % ==
}
close(c3)
}() for v := range c3 {
fmt.Println(v)
}
unbuffered channel
下面这两种声明是一样的
c1 := make(chan bool, ) // unbuffered channel
c2 := make(chan bool) // unbuffered channel
unbuffered channel 的接收channel会一直阻塞,直到有值传给channel, 也可以说发送channel会一直阻塞,至到有接收channel
c1 := make(chan bool, ) // unbuffered channel
c2 := make(chan bool) // unbuffered channel go func() {
c1 <- false
time.Sleep(time.Second * )
c2 <- true
}() fmt.Println(<-c1)
fmt.Println(<-c2)
相关讲解:
https://golang.org/doc/effective_go.html
定义类型和组合类型的区别
定义类型,也可以说是别名和组合类型的区别
有一个Test的结构,NewTest是以Test为类型的一个定义,New2Test和New3Test都是组合类型
type Test struct { N int }
func (m *Test) Name() { fmt.Println("abc")} // NewTest does not inherit any functions of Test
// can access fields
type NewTest Test // New2Test is composite type, it inherit all functions of Test
// can access fields
type New2Test struct {
Test
} // if embedded type is pointer you must initialized it
type New3Test struct {
*Test
}
1.定义类型NewTest 相当于一个新的类型,他不能直接调用Test的方法,但是可以访问Test的字段。如果想调用原类型的方法需要做转换
2.New2Test和New3Test都是组合类型,他俩都可以直接调用Test的方法和访问Test的字段,他俩的不同之处就是一个是值组合一个是指针组合
3.在实例化New3Test的时候需要手动实例化*Test指针
n := NewTest{}
n.N =
// n have no method
// n.Name() // error
v := (*Test)(&n)
v.Name() v2 := Test(n)
v2.Name() n2 := New2Test{}
n2.N =
n2.Name() n3 := New3Test{Test: new(Test)}
// access filed N will panic if you do not initialized *Test
n3.N =
n3.Name()
相关的解答:
https://golang.org/ref/spec#Type_declarations
---
golang 常见疑惑总结的更多相关文章
- golang 常见变成问题01
Golang常见编程问题 --语言简单 func CopyFile (dst, src string) (w int64, err error) { srcFile, err := os.Open ( ...
- Golang 常见设计模式之单例模式
之前我们已经看过了 Golang 常见设计模式中的装饰和选项模式,今天要看的是 Golang 设计模式里最简单的单例模式.单例模式的作用是确保无论对象被实例化多少次,全局都只有一个实例存在.根据这一特 ...
- Golang 常见设计模式之选项模式
熟悉 Python 开发的同学都知道,Python 有默认参数的存在,使得我们在实例化一个对象的时候,可以根据需要来选择性的覆盖某些默认参数,以此来决定如何实例化对象.当一个对象有多个默认参数时,这个 ...
- Golang常见误区(二)
35. 关闭 HTTP 的响应体 使用 HTTP 标准库发起请求.获取响应时,即使你不从响应中读取任何数据或响应为空,都需要手动关闭响应体.新手很容易忘记手动关闭,或者写在了错误的位置: // 请求失 ...
- Linux网络编程系列-常见疑惑
1.并发TCP最大连接数 一个TCP连接有一个四元组唯一标识{local_ip, local_port, remote_ip, remote_port} client端建立连接请求时,通常让系统分配一 ...
- Golang常见误区(一)
1.左大括号一般不能单独一行 在其他大多数语言中,{ 的位置你自行决定.Go 比较特别,遵守分号注入规则(automatic semicolon injection):编译器会在每行代码尾部特定分隔符 ...
- golang常见错误
import import unuse package: error : imported and not used: "os" := = c := 1 // error non- ...
- Java语法基础常见疑惑解答8,16,17,21图片补充
8. 16. 17. 21
- Java语法基础常见疑惑解答
1. 类是java的最小单位,java的程序必须在类中才能运行 2. java函数加不加static有何不同 java中声明为static的方法称为静态方法或类方法.静态方法可以直接调用静态方法,访问 ...
随机推荐
- Apache Commons Configuration读取xml配置
近期项目自己手写一个字符串连接池.因为环境不同有开发版本.测试版本.上线版本.每一个版本用到的数据库也是不一样的.所以需要能灵活的切换数据库连接.当然这个用maven就解决了.Apache Commo ...
- Fork And Join框架初探
首先,关于Fork And Join框架的入门资料我觉得最好的是: Java线程(十一):Fork/Join-Java并行计算框架 本文参考了这篇文章 Fork/Join框架的核心类是ForkJoin ...
- BFS与DFS总结
最近一直在看DFS和BFS,感觉要晕的GJ. DFS思想: 一直往深处走,直到找到解或者走不下去为止 DFS框架: DFS(dep,-) //dep代表目前DFS的深度 { if (找到 ...
- 【IOS 开发】Object - C 语法 之 流程控制
1. if 条件语句 if 表达式 : 表达式是一个 整型 或者 布尔型, 0 或者 FALSE 为 FALSE, 大于 0 为 TRUE; 代码示例 : /********************* ...
- Struts2进阶(一)运行原理及搭建步骤
Struts2进阶(一)运行原理 Struts2框架 Struts2框架搭建步骤 致力于web服务,不可避免的涉及到编程实现部分功能.考虑使用到SSH框架中的Struts2.本篇文章只为深入理解Str ...
- JAVA数组的定义以及使用1
public class HelloWorld { public static void main(String[] args){ // Scanner s = new Scanner(System. ...
- Chapter 2 User Authentication, Authorization, and Security(5):使用固定服务器角色
原文出处:http://blog.csdn.net/dba_huangzj/article/details/38844999,专题目录:http://blog.csdn.net/dba_huangzj ...
- UNIX环境高级编程——文件和目录
一.获取文件/目录的属性信息 int stat(const char *path, struct stat *buf); int fstat(int fd, struct stat *buf); in ...
- python爬虫 - Urllib库及cookie的使用
http://blog.csdn.net/pipisorry/article/details/47905781 lz提示一点,python3中urllib包括了py2中的urllib+urllib2. ...
- [SqlServer]2008转到2005的步骤步骤
2008转到2005的步骤步骤 1. 生成for 2005版本的数据库脚本 2005 的manger studio -- 打开"对象资源管理器"(没有的话按F8), 连接到你的实例 ...