Github 开源项目(二)gorun (go语言工具)

gorun是一个工具,可以在Go程序的源代码中放置“爆炸线”来运行它,或者明确运行这样的源代码文件。 它的创建旨在试图让Go更加吸引那些习惯于Python和类似语言的人们,他们使用源代码进行最明显的操作。

项目地址:https://github.com/erning/gorun

一、安装环境:

系统:Ubuntu 16.04.3 LTS

Go 版本:go1.9.1

如果没有安装好go 环境,请先安装go环境

sudo add-apt-repository ppa:gophers/go 
sudo apt-get update  
sudo apt-get install golang

使用git 安装gorun

go get github.com/erning/gorun

安装结果:

./gorun 
usage: gorun <source file> [...]

做个软连接,可以全局使用

sudo ln -s /home/www/golang/gopath/bin/linux_386/gorun /usr/bin/gorun

编写脚本 gorun_script.go

cat gorun_script.go 
#!/usr/bin/env gorun

package main

func main() {
    println("Hello gorun!")
}

赋予指定权限

chmod u+x gorun_script.go

执行该脚本

./gorun_script.go 
Hello gorun!

二、性能测试(python比较)

 测试文件:hello.go

cat hello.go 
package main
import "fmt"
func main() {
    fmt.Println("Hello World!")
}

go执行结果

www@TinywanAliYun:~/golang$ time gorun hello.go
Hello World!

real    0m0.199s
user    0m0.152s
sys    0m0.020s
www@TinywanAliYun:~/golang$ time gorun hello.go
Hello World!

real    0m0.002s
user    0m0.000s
sys    0m0.000s

python执行结果

www@TinywanAliYun:~/golang$ time python -c 'print "Hello world123!"'
Hello world123!

real    0m0.010s
user    0m0.008s
sys    0m0.000s
www@TinywanAliYun:~/golang$ time python -c 'print "Hello world123!"'
Hello world123!

real    0m0.011s
user    0m0.008s
sys    0m0.004s

php 语言

www@TinywanAliYun:~/golang$ time php hellp.php 
Hello World
real    0m0.019s
user    0m0.012s
sys    0m0.004s
www@TinywanAliYun:~/golang$ time php hellp.php 
Hello World
real    0m0.020s
user    0m0.004s
sys    0m0.012s

请注意 gorun 第二次运行的速度比第一次快得多。 发生这种情况是因为在第一次编译后使用了该文件的缓存版本。

gorun会在需要时正确重新编译文件。

 

posted @ 2018-03-01 16:00  Tinywan  阅读(1423)  评论(0编辑  收藏  举报