有人在stackoverflow上面提问:
http://stackoverflow.com/questions/17776584/webserver-for-go-golang-webservices-using-nginx-or-not
- I am writing some webservices returning JSON data, which have lots of users.
- Would you recommend to use NGINX as a webserver or it is good enough to use the standard http server of Go?
于是有人就回答了:
XML/HTML代码
- It depends.
-
- Out of the box, putting nginx in front as a reverse proxy is going to give you:
-
- Access logs
- Error logs
- Easy SSL termination
- SPDY support
- gzip support
- Easy ways to set HTTP headers for certain routes in a couple of lines
- Very fast static asset serving (if you're serving off S3/etc. though, this isn't that relevant)
- The Go HTTP server is very good, but you will need to reinvent the wheel to do some of these things (which is fine: it's not meant to be everything to everyone).
-
- I've always found it easier to put nginx in front—which is what it is good at—and let it do the "web server" stuff. My Go application does the application stuff, and only the bare minimum of headers/etc. that it needs to. Don't look at putting nginx in front as a "bad" thing.
还有人回答:
XML/HTML代码
- The standard http server of Go is fine. If your application mostly/only are "dynamic" requests/responses, then it's really the best way.
-
- You could use nginx to serve static assets, but most likely the standard Go one is fine for that, too. If you need higher performance you should just use a CDN or cache as much as you can with Varnish (for example).
-
- If you need to serve different applications off the same IP address, nginx is a fine choice for a proxy to distribute requests between the different applications; though I'd more often get Varnish or HAProxy out of the toolbox for that sort of thing.
这回你觉得呢?你还会用nginx吗?还是只用go做http server/???
不要犹豫了,上海首届golang聚会就这样悄悄的开始了。Think2Go戈登营(Think技术社区Go语言Camp)首期,再晚你就可能没有机会参加首届聚会了,这次只有30个人,是在一个小小的咖啡馆里举行的。
话题主要由3位达人进行分享:
《开场》 Think社区
《Go语言在CDN下载系统中的应用》谢孟军 盛大云 高级研究员
《Go在微博数据分析中的应用》邵天宇 Buzzreader 高级工程师
《golang与高强度在线服务》韩拓,七牛云 CTO
《OpenTalk》所有参与者
是不是觉得很心动?参加地点其实也不算远(对上海的朋友来说),地方很好找。点评网上也有介绍:http://www.dianping.com/shop/6630958,直接点过去看看就OK了。详细地址在:上海市卢湾区蒙自东路63号(近马当路地铁站3号口)
费用不算太高,98元,应该是都能够承受的价格了。毕竟占用了咖啡店30个座位,别人也要做生意的。。。
上传一张成功的订座票。处女票,值得收藏。。。可惜没有实体的
谢大的教程在:https://github.com/astaxie/build-web-application-with-golang
在复刻和学习的过程中,把问题记录下来的笔记
1、
XML/HTML代码
- // 声明一个数组
- var array = [10]byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'}
- // 声明两个slice
- var aSlice, bSlice []byte
-
- // 演示一些简便操作
- aSlice = array[:3] // 等价于aSlice = array[0:3] aSlice包含元素: a,b,c
- aSlice = array[5:] // 等价于aSlice = array[5:10] aSlice包含元素: f,g,h,i,j
- aSlice = array[:] // 等价于aSlice = array[0:10] 这样aSlice包含了全部的元素
-
- // 从slice中获取slice
- aSlice = array[3:7] // aSlice包含元素: d,e,f,g,len=4,cap=7
- bSlice = aSlice[1:3] // bSlice 包含aSlice[1], aSlice[2] 也就是含有: e,f
- bSlice = aSlice[:3] // bSlice 包含 aSlice[0], aSlice[1], aSlice[2] 也就是含有: d,e,f
- bSlice = aSlice[0:5] // 对slice的slice可以在cap范围内扩展,此时bSlice包含:d,e,f,g,h
- bSlice = aSlice[:] // bSlice包含所有aSlice的元素: d,e,f,g
主要是加深颜色的那句。。。。
2、
go代码
- type human struct {
- name string
- age string
- height int
- }
- type student struct {
- human
- skills
- int
- age int
- speciality string
- }
- func (h *human) sayhi() {
- fmt.Printf("human say hello, i'm %s \n", h.name)
- }
- func (h *human) gogo() {
- fmt.Println("gogogo");
- }
- type men interface{
- sayhi()
- gogo()
- }
-
- 然后用的地方:
- mike := student{human:human{name:"mike"}}
- var i men;
- i = mike;
- i.sayhi();
因为这是早期的所见所得编辑器。对于代码高亮做的不好。忍忍吧。。。
上面的代码是会出错的。
cannot use mike (type student) as type men in assignment:
student does not implement men (gogo method requires pointer receiver)
需要:mike := &student{human:human{name:"mike"}}
看到没,在student前有个取址符。主要是因为方法指定的是:(h *human),如果是(h human),就不需要取址符了
*human 是指针,但是你mike不是指针,只是对象,所以没有实现这两个方法,才会报错
然后谢大给出了这个:http://segmentfault.com/q/1010000000198984#a-1020000000199002,说是里面有解释
XML/HTML代码
- 假设T是struct,那么Go里面遵循下面几个原则:
-
- T的方法集仅拥有 T Receiver 方法。
- *T 方法集则包含全部方法 (T + *T)。
- 所以你上面的例子dept1应该是拥有方法:Name和SetName
- 而&dept1拥有方法:Name、SetName和Relocate
- 这个就是Go里面在设计方法的时候需要注意Receiver的类型
在群里突然看到一段代码:
XML/HTML代码
- package main
-
- import (
- "fmt"
- )
-
- var DomainId int
-
- func init() {
- DomainId, err := GetDomainId()
- if err != nil {
- DomainId = -1
- }
- fmt.Println(DomainId)
- }
-
- func GetDomainId() (int, error) {
- DomainId = 1
- return DomainId, nil
- }
注意看红色背景的一条,理论上这段代码没有错,但事实上会报错了。
XML/HTML代码
- [上海]Asta谢() 22:29:50
- 我知道
- [上海]Asta谢() 22:29:55
- 我踩过这个坑
- [上海]Asta谢() 22:30:04
- init里面不能用:=
所以,上面的代码应该是写成:
XML/HTML代码
- func init() {
- var err error
- DomainId, err = GetDomainId()
- if err != nil {
- DomainId=-1
- }
- }
对比两段红色背景的代码。主要是做个笔记 .怕会忘 .
Asta谢 是谁?看这里:https://github.com/astaxie/build-web-application-with-golang ,这里有一篇他的教程,适合广大人民群众查看