关于golang的一些基础

说实话,golang挺简单的,像我这样的低级的编程水平学起来感觉不太吃力。goroutine + channel挺开眼界的,这样的http并发模型秒杀python。

…我第一次知道type其实和struct是可以分开的。

golang 循环都是用for语句

for initialization; condition; post {
    // zero or more statements
}
// a traditional "while" loop
for condition {
    // ...
}

golang分支语句if
golang switch语句

switch coinflip() {
case "heads":
    heads++
case "tails":
    tails++
default:
    fmt.Println("landed on edge!")
}

golang range的意思?
Go 语言中 range 关键字用于 for 循环中迭代数组(array)、切片(slice)、通道(channel)或集合(map)的元素。在数组和切片中它返回元素的索引和索引对应的值,在集合中返回 key-value 对。

golang中_可以用来表示不需要使用的变量

strings.Join和 字符串+之间的区别

golang 里面的channel是什么意思?

	ch := make(chan string)
		go fetch(url, ch) // start a goroutine
	fmt.Println(<-ch) // receive from channel ch

//在fecth函数中中		
ch <- fmt.Sprintf("%.2fs  %7d  %s", secs, nbytes, url)

当一个goroutine尝试在一个channel上做send或者receive操作时,这个goroutine会阻塞在调用处,直到另一个goroutine从这个channel里接收或者写入值,这样两个goroutine才会继续执行channel操作之后的逻辑。

go 函数名 创建一个goroutine
golang里面<-是什么意思?
send 或者receive

go net/http是怎么处理并发的?
使用了goroutine并发处理。
服务器每一次接收请求处理时都会另起一个goroutine,这样服务器就可以同一时间处理多个请求

golang访问权限分为包内访问,和包外访问。
如果首字母大写,包外可以访问;如果首字母小写,只能在本包内访问。

golang中interface和struct的区别是什么?
interface vs struct?

defer是什么意思?

panic是什么意思?

下面我没有理解。
这是什么语法,我有点不太理解了

接收者的概念。
方法是带着接收者的函数
在这里插入图片描述
举个例子就好理解了
ff := func(ResponseWriter, *Request){
fmt.Println(“test”)
return
}
ff.ServeHTTP(w, r)

posted @ 2022-03-06 10:37  叶常落  阅读(2)  评论(0编辑  收藏  举报