体验下 Go 语言的魅力(初试)

这篇是初体验,欢迎看我另外一篇go的学习 click mecss

关于 Go 语言的好处简单说两点吧。

上述为Donng所陈述(能够跳转到那里查看)mysql

我就简单说一下怎末让他简单跑起来linux

首先就是你要有一个go的环境啦!git

https://golang.google.cn/dl/   
// 去这个地方下载安装 go的环境
// windows: 后缀 msi
// linux: gz
// mac: pkg
//FreeBSD: gz
复制代码

ps:我是直接使用的 beego框架github

go get github.com/astaxie/beego  // beego 框架
go get github.com/beego/bee  // bee 工具
复制代码

测试是否安装成功golang

控制台 cmd/powershell/vscode   输入  bee
// 下面是bee的功能
Bee is a Fast and Flexible tool for managing your Beego Web Application.

Usage:

    bee command [arguments]

The commands are:

    version     show the bee & beego version
    migrate     run database migrations
    api         create an api application base on beego framework
    bale        packs non-Go files to Go source files    
    new         create an application base on beego framework
    run         run the app which can hot compile
    pack        compress an beego project
    fix         Fixes your application by making it compatible with newer versions of Beego
    dlv         Start a debugging session using Delve
    dockerize   Generates a Dockerfile for your Beego application
    generate    Source code generator
    hprose      Creates an RPC application based on Hprose and Beego frameworks
    new         Creates a Beego application
    pack        Compresses a Beego application into a single file
    rs          Run customized scripts
    run         Run the application by starting a local development server
    server      serving static content over HTTP on port
    
Use bee help [command] for more information about a command.
复制代码

安装完以后 beego 提供了几种快速搭建方法以下sql

new命令:bee new myproject(name)

new 命令是新建一个 Web 项目,咱们在命令行下执行 bee new <项目名> 就能够建立一个新的项目。可是注意该命令必须在 $GOPATH/src 下执行。最后会在 $GOPATH/src 相应目录下生成以下目录结构的项目:docker

bee new myproject
[INFO] Creating application...
/gopath/src/myproject/
/gopath/src/myproject/conf/
/gopath/src/myproject/controllers/
/gopath/src/myproject/models/
/gopath/src/myproject/static/
/gopath/src/myproject/static/js/
/gopath/src/myproject/static/css/
/gopath/src/myproject/static/img/
/gopath/src/myproject/views/
/gopath/src/myproject/conf/app.conf
/gopath/src/myproject/controllers/default.go
/gopath/src/myproject/views/index.tpl
/gopath/src/myproject/main.go
13-11-25 09:50:39 [SUCC] New application successfully created!
复制代码
myproject
├── conf
│   └── app.conf
├── controllers
│   └── default.go
├── main.go
├── models
├── routers
│   └── router.go
├── static
│   ├── css
│   ├── img
│   └── js
├── tests
│   └── default_test.go
└── views
    └── index.tpl

8 directories, 4 files
复制代码

api命令:bee api apiproject(name)

上面的 new 命令是用来新建 Web 项目,不过不少用户使用 beego 来开发 API 应用。因此这个 api 命令就是用来建立 API 应用的,执行命令以后以下所示:shell

bee api apiproject
create app folder: /gopath/src/apiproject
create conf: /gopath/src/apiproject/conf
create controllers: /gopath/src/apiproject/controllers
create models: /gopath/src/apiproject/models
create tests: /gopath/src/apiproject/tests
create conf app.conf: /gopath/src/apiproject/conf/app.conf
create controllers default.go: /gopath/src/apiproject/controllers/default.go
create tests default.go: /gopath/src/apiproject/tests/default_test.go
create models object.go: /gopath/src/apiproject/models/object.go
create main.go: /gopath/src/apiproject/main.go
复制代码
apiproject
├── conf
│   └── app.conf
├── controllers
│   └── object.go
│   └── user.go
├── docs
│   └── doc.go
├── main.go
├── models
│   └── object.go
│   └── user.go
├── routers
│   └── router.go
└── tests
    └── default_test.go
复制代码

从上面的目录咱们能够看到和 Web 项目相比,少了 static 和 views 目录,多了一个 test 模块,用来作单元测试的。数据库

同时,该命令还支持一些自定义参数自动链接数据库建立相关 model 和 controller: bee api [appname] [-tables=""] [-driver=mysql] [-conn="root:<password>@tcp(127.0.0.1:3306)/test"] 若是 conn 参数为空则建立一个示例项目,不然将基于连接信息连接数据库建立项目。

启动命令:bee run

bee run
13-11-25 09:53:04 [INFO] Uses 'myproject' as 'appname'
13-11-25 09:53:04 [INFO] Initializing watcher...
13-11-25 09:53:04 [TRAC] Directory(/gopath/src/myproject/controllers)
13-11-25 09:53:04 [TRAC] Directory(/gopath/src/myproject/models)
13-11-25 09:53:04 [TRAC] Directory(/gopath/src/myproject)
13-11-25 09:53:04 [INFO] Start building...
13-11-25 09:53:16 [SUCC] Build was successful
13-11-25 09:53:16 [INFO] Restarting myproject ...
13-11-25 09:53:16 [INFO] ./myproject is running...
复制代码

咱们打开浏览器就能够看到效果 http://localhost:8080/:

其余还请参考 beego 官方文档:

这里跳转beego官方

相关文章
相关标签/搜索