张凯朝

张凯朝的博客

他的个人主页  他的博客

go language

张凯朝  2010年04月27日 星期二 13:36 | 13178次浏览 | 1条评论

tags: go

差不多一个月前我仔细看了 [Tim](http://timyang.net) 的
[C, Erlang, Java and Go Web Server performance test](http://timyang.net/programming/c-erlang-java-performance/),
并在我自己的笔记本上跟着使用 ab 做了一些测试,
其中还测试了两个 Python 的 web 框架,
[web.py](http://webpy.org) 和 [Tornado](http://github.com/facebook/tornado) ,
web.py 弱得简直不堪使用了,
而在 [Asynchronous Servers in Python](http://nichol.as/asynchronous-servers-in-python)
测试中性能强悍的 Tornado 应该算是我所知道的 Python web 框架中较好的一个了,
不过也不是很好,对比其他非 Python 的 web server ,哈哈。
go 的并发性能还是不错的,这让我很想学习各种 go 的应用。
于是我下载了几个跟 go 相关项目的源代码,
包括
[web.go](http://github.com/hoisie/web.go),
[Go-Redis](http://github.com/alphazero/Go-Redis),
[gomemcached](http://github.com/dustin/gomemcached),
[gosqlite3](http://github.com/salviati/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](http://www.infoq.com/cn/articles/google-go-primer)):

    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://n23.appspot.com/blog/post/37401)

--
http://magicoding.appspot.com/entry/1

评论

我的评论:

发表评论

请 登录 后发表评论。还没有在Zeuux哲思注册吗?现在 注册 !
小包

回复 小包  2010年04月27日 星期二 14:45

给我的感觉 GO 感觉太乱了。。。 比JAVA 代码都还难看,又长有难记

0条回复

暂时没有评论

Zeuux © 2024

京ICP备05028076号