體驗下 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官方

相關文章
相關標籤/搜索