2010年5月22日星期六

go language

差不多一个月前我仔细看了 Tim C, Erlang, Java and Go Web Server performance test, 并在我自己的笔记本上跟着使用 ab 做了一些测试, 其中还测试了两个 Python 的 web 框架, web.py Tornado , web.py 弱得简直不堪使用了, 而在Asynchronous Servers in Python 测试中性能强悍的 Tornado 应该算是我所知道的 Python web 框架中较好的一个了, 不过也不是很好,对比其他非 Python 的 web server ,哈哈。 go 的并发性能还是不错的,这让我很想学习各种 go 的应用。 于是我下载了几个跟 go 相关项目的源代码, 包括 web.go Go-Redisgomemcached gosqlite3。 无一例外的,都不能编译成功使用。

其中最常见的一个问题是,我的 go 环境对“;”的严格要求, 而这些新的项目是在比较新的 go 环境中开发的,很多地方已不需要添加“;”。 话说我的 go 版本比较低,是去年年底 2009,12,11 的源码编译的。 于是我开始一个个地给那些我的 go 环境需要“;”的地方添加“;”, 然而这是一件浩大的工程。。。遂放弃了。。。

心想,go 语言也真是的,这么普通的规则也更改了, 哪天真的用其做了一些东西出来,升级岂不是要整死人了?

不过真的很看好 go 语言,等我有了比较好的网络环境了再更新 go 的源代码, 领略一下这些构建在优秀的 go 语言上的新项目的魅力吧!

附:

一个简单的 Hello world 的 go server :

package main

import (
"http";
"io";
"runtime";
)

func HelloServer(c *http.Conn, req *http.Request) {
io.WriteString(c, "hello, world!\n");
}

func main() {
runtime.GOMAXPROCS(2); // 2 cores
http.Handle("/", http.HandlerFunc(HelloServer));
err := http.ListenAndServe(":8080", nil);
if err != nil {
panic("ListenAndServe: ", err.String())
}
}

一个粗糙的 fibonacci :

package main

import "fmt"

func main() {
var a uint8 = 0;
var b uint8 = 1;

for i :=0; i<10; i++ {
a, b = b, a + b;
fmt.Printf("%d\n", a);
}
}

一个简单的 channel 例子 (摘自 infoq-cn):

package main

import "fmt"

func main() {
ch := make(chan int);

go func() {
result := 0;
for i:=0; i<100000000; i++ {
result = result + i
}
ch <- result;
}();

/* Do something for a while */
fmt.Println("Have a rest!");
fmt.Println("Have a rest, too!");

sum := <- ch; // This will block if the calculation is not done yet.
fmt.Println("The sum is:", sum);

/* create a channel with buffer size 5 . */
ch2 := make(chan int, 5);
/* Send without blocking, ok will be true if value was buffered. */
ok := ch2 <- 42;
/* Read without blocking, ok will be true if a value was read. */
val, ok := <-ch2;
fmt.Println("ok is:", ok);
fmt.Println("val is:", val);
}

一篇比较全面介绍 Go 语言的文章: Go Lang介绍

--
http://magicoding.appspot.com/entry/1
2010,04,27

1 条评论: