Skip to content

Instantly share code, notes, and snippets.

@novohool
Last active June 17, 2018 11:16
Show Gist options
  • Save novohool/d53fb58f2c9a00f9f17ca6baf44d592f to your computer and use it in GitHub Desktop.
Save novohool/d53fb58f2c9a00f9f17ca6baf44d592f to your computer and use it in GitHub Desktop.
用go 写web程序,好处是编译型程序生成独立二进制文件,看不到源码,程序可跨平台,web本身也跨平台
- 下载安装golang
- 安装beego
把C:\Go\bin\设置为系统变量%GOPATH%
把%GOPATH%和%GOPATH%\bin都加到path中,命令行执行
```
go get -u github.com/astaxie/beego
go get -u github.com/beego/bee
```
- 创建测试程序hello
新建个项目文件,在项目文件中新建个文件hello.go,代码为:
```
package main
import (
"github.com/astaxie/beego"
)
type MainController struct {
beego.Controller
}
func (this *MainController) Get() {
this.Ctx.WriteString("hello world")
}
func main() {
beego.Router("/", &MainController{})
beego.Run()
}
```
- 执行编译
```golang
go build -o hello.exe hello.go
```
双击运行hello.exe即可 打开127.0.0.1:8080就可以看到程序了。
- 用bee创建web项目
```bat
cd C:\Go\bin\src
bee new myweb.com
cd myweb.com
调试程序
bee run myweb.com
打包程序
bee pack -be GOOS=linux GOARCH=amd64
```
- 运行更改别人的项目
比如这个:
[网页端定时的任务](https://github.com/lisijie/webcron)
是为linux下服务器开发的定时任务,目前没有windows版本
这里修改为windows版本
- 先拉取源码
- 获取源码
```
$ go get github.com/lisijie/webcron
```
打开配置文件 conf/app.conf,修改相关配置。
创建数据库webcron,再导入install.sql
```
$ mysql -u username -p -D webcron < install.sql
```
```
cd C:\Go\bin\src\github.com\lisijie\webcron\app\jobs
```
找到exec.Command这行,修改为
```
cmd := exec.Command("cmd", "/C",command)
```
cd 回退到lisijie\webcron项目根目录,编译打包
```
cd ../..
bee pack
```
这个文件就是我们要的文件webcron.tar.gz,解压运行即可
- 访问:
http://localhost:8000
帐号:admin
密码:admin888
- 编译好的附件:
```
链接:https://pan.baidu.com/s/1a5TAfLT3cEkUoc39DRHVXA 密码:beqf
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment