1. 程式人生 > 實用技巧 >CentOS7安裝GoLang環境及BeeGo框架

CentOS7安裝GoLang環境及BeeGo框架

一、 安裝GoLang執行環境

  1. 下載golang包

    wget https://golang.google.cn/doc/install?download=go1.15.3.linux-amd64.tar.gz
  2. 解壓

    sudo tar -C /usr/local -xzf go1.15.3.linux-amd64.tar.gz
  3. 配置環境

    sudo vim /etc/profile.d/go.sh

    輸入

    export PATH=$PATH:/usr/local/go/bin

    按ESC => wq!退出並儲存

  4. 讓配置生效

    source /etc/profile.d/go.sh

二、 安裝beego環境

  1. 配置代理環境,以下是代理地址,配置後可以提升下載速度,配置任意一個即可。

    export GOPROXY=https://mirrors.aliyun.com/goproxy/
    export GOPROXY=https://proxy.golang.org
    export GOPROXY=https://goproxy.io
    sudo vim /etc/profile.d/go.sh
    配置如下:
    export PATH=$PATH:/usr/local/go/bin
    export GOPROXY="https://goproxy.io"
    然後source /etc/profile.d/go.sh,讓配置生效
  2. 下載beego包

    go get github.com/astaxie/beego
  3. 下載bee包

    go get github.com/beego/bee
  4. 配置bee目錄環境

    sudo vim /etc/profile.d/go.sh
    配置如下:

    export GOROOT=/usr/local/go
    export GOPATH=/home/zhuzi/go
    export GOBIN=$GOPATH/bin
    export PATH=$PATH:$GOROOT/bin:$GOBIN

    export GO111MODULE="on"
    export GOPROXY="https://goproxy.io"

    然後source /etc/profile.d/go.sh,讓配置生效
  5. 建立第一個beego專案

    進入目錄/home/zhuzi/go/src
    cd /home/zhuzi/go/src

    利用bee命令建立helloworld專案
    bee new helloworld

    控制檯輸出以下內容

         create  /home/zhuzi/go/src/helloworld/go.mod
    create /home/zhuzi/go/src/helloworld/
    create /home/zhuzi/go/src/helloworld/conf/
    create /home/zhuzi/go/src/helloworld/controllers/
    create /home/zhuzi/go/src/helloworld/models/
    create /home/zhuzi/go/src/helloworld/routers/
    create /home/zhuzi/go/src/helloworld/tests/
    create /home/zhuzi/go/src/helloworld/static/
    create /home/zhuzi/go/src/helloworld/static/js/
    create /home/zhuzi/go/src/helloworld/static/css/
    create /home/zhuzi/go/src/helloworld/static/img/
    create /home/zhuzi/go/src/helloworld/views/
    create /home/zhuzi/go/src/helloworld/conf/app.conf
    create /home/zhuzi/go/src/helloworld/controllers/default.go
    create /home/zhuzi/go/src/helloworld/views/index.tpl
    create /home/zhuzi/go/src/helloworld/routers/router.go
    create /home/zhuzi/go/src/helloworld/tests/default_test.go
    create /home/zhuzi/go/src/helloworld/main.go
    2020/10/21 11:44:48 SUCCESS ▶ 0003 New application successfully created!

    進入helloworld專案根目錄

    cd helloworld/

    啟動helloworld專案

    bee run

    控制檯輸出以下內容

    ______
    | ___ \
    | |_/ / ___ ___
    | ___ \ / _ \ / _ \
    | |_/ /| __/| __/
    \____/ \___| \___| v1.11.0
    2020/10/21 11:44:57 INFO ▶ 0001 Using 'helloworld' as 'appname'
    2020/10/21 11:44:57 INFO ▶ 0002 Initializing watcher...
    go: downloading gopkg.in/yaml.v2 v2.2.1
    go: finding module for package github.com/shiena/ansicolor
    go: downloading github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18
    go: found github.com/shiena/ansicolor in github.com/shiena/ansicolor v0.0.0-20200904210342-c7312218db18
    github.com/astaxie/beego/config
    gopkg.in/yaml.v2
    github.com/shiena/ansicolor
    github.com/astaxie/beego/session
    github.com/astaxie/beego/logs
    github.com/astaxie/beego/grace
    github.com/astaxie/beego/context
    github.com/astaxie/beego/toolbox
    github.com/astaxie/beego/context/param
    github.com/astaxie/beego
    helloworld/controllers
    helloworld/routers
    helloworld
    2020/10/21 11:45:10 SUCCESS ▶ 0003 Built Successfully!
    2020/10/21 11:45:10 INFO ▶ 0004 Restarting 'helloworld'...
    2020/10/21 11:45:10 SUCCESS ▶ 0005 './helloworld' is running...
    2020/10/21 11:45:10.538 [I] [asm_amd64.s:1374] http server Running on http://:8080

    到此為止,GoLang環境及Beego框架安裝和配置完成