wget https://dl.google.com/go/go1.12.4.linux-amd64.tar.gz

sudo tar -zxvf go1.12.4.linux-amd64.tar.gz -C /opt

检查安装是否成功

cd /opt/go/bin

./go version

go version go1.12.4 linux/amd64

设置环境变量

vi /etc/profile

export GOROOT=/opt/go
export GOPATH=/home/ubuntu/go
export GOPROXY=https://goproxy.io
export GOARCH=amd64
export GOOS=linux
export GOTOOLS=$GOROOT/pkg/tool
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin source /etc/profile

其中,GOPROXY 可以解决 golang.org/x/... 系列包无法下载的问题。

编写 HelloWorld 文件,测试运行环境。

package main
import "fmt"
func main(){
fmt.Println("hello,world!")
}
go run hello.go
go build hello.go

创建 go.mod 文件

go mod init hello

用 Gin 实现一个简单的 http 服务

import (
"gopkg.in/gin-gonic/gin.v1"
"net/http"
) func main(){ router := gin.Default() router.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "Hello World")
})
router.Run(":8000")
}

直接编译执行

go run hello

可以看到引用的包都被自动下载了

go: finding github.com/gin-contrib/sse latest
go: finding github.com/gin-gonic/gin/render latest
go: finding github.com/gin-gonic/gin/binding latest
go: finding github.com/gin-gonic/gin/json latest
go: finding golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223
go: downloading golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223
go: finding gopkg.in/go-playground/validator.v8 v8.18.2
go: finding github.com/ugorji/go/codec latest
go: finding github.com/golang/protobuf/proto latest
go: finding gopkg.in/yaml.v2 v2.2.2
go: downloading gopkg.in/go-playground/validator.v8 v8.18.2
go: downloading gopkg.in/yaml.v2 v2.2.2
go: finding github.com/ugorji/go v1.1.4
go: finding github.com/golang/protobuf v1.3.1
go: downloading github.com/ugorji/go v1.1.4
go: downloading github.com/golang/protobuf v1.3.1
go: extracting gopkg.in/go-playground/validator.v8 v8.18.2
go: extracting gopkg.in/yaml.v2 v2.2.2
go: extracting github.com/golang/protobuf v1.3.1
go: extracting github.com/ugorji/go v1.1.4
go: extracting golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223
go: finding gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405

HTTP 服务启动正常,可以通过浏览器访问了。

[GIN-debug] [WARNING] Now Gin requires Go 1.6 or later and Go 1.7 will be required soon.

[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode) [GIN-debug] GET / --> main.main.func1 (3 handlers)
[GIN-debug] Listening and serving HTTP on :8000
 

Ubuntu 安装最新版 (1.12) Golang 并使用 go mod的更多相关文章

  1. Ubuntu 安装最新版nodejs

    转自:ubuntu快速安装最新版nodejs,只需2步 第一步,去 nodejs 官网 https://nodejs.org 看最新的版本号: 也就是说此时此刻,12.6.0 是最新的版本,不过你求稳 ...

  2. Ubuntu安装最新版的nodejs

    安装玩Ubuntu的虚拟机之后安装nodejs发现npm的版本才3.5.2,这都多老了?于是Google了一下,发现是由于Ubuntu官方维护的包源太老了,想要安装nodejs的最新版,两种方法,一种 ...

  3. Ubuntu安装最新版nodejs

    今天在学习以太坊时,需要用到nodejs,因为使用的是ubuntu 16.04 LTS,一直安装的是老版本的nodejs,官方给方法用不成,折腾了半天,什么软链.手动编译,总觉得不很靠谱(linux水 ...

  4. ubuntu -- 安装最新版的nodejs

    1.安装最新的nodejs和npm # apt-get update # apt-get install -y python-software-properties software-properti ...

  5. ubuntu安装最新版node和npm

    1.先在系统上安装好nodejs和npm           sudo apt-get install nodejs-legacy sudo apt-get install npm     2.升级n ...

  6. [190308]Ubuntu 安装完之后,安装的软件小记

    install software vim sudo apt-get install -y vim Typora command copy from Typora website # or run: # ...

  7. Ubuntu 14.04中安装最新版Eclipse

    Ubuntu 14.04中安装最新版Eclipse 来源:Linux社区    作者:Linux 1.安装OpenJDK Java 7 如果你的系统中没有安装Java,我们需要按照如下步骤事先安装好 ...

  8. Ubuntu安装Navicat 12 for MySQL

    环境准备 要想运行Navicat,必须先安装Wine,这个可以使用下面的命令来安装Wine: ubuntu@ubuntu ~ $ sudo apt-get install wine-stable 安装 ...

  9. Win10系统XWware虚拟机安装Linux系统(Ubuntu)最新版教程

    XWware虚拟机安装Linux系统(Ubuntu)教程 一.下载并安装VMware虚拟机 借助VMware Workstation Pro, 我们可以在同一台Windows或Linux PC上同时运 ...

随机推荐

  1. k8s几种pod的控制器

    replicationcontroller 选择器 模版 副本数   如果更改选择器,则会创建新的pod 如果更改pod的标签,那么也会创建新的pod进行替换,但是老的pod不会被删除 如果更改模版, ...

  2. 爬虫之selenium模块;无头浏览器的使用

    一,案例 爬取站长素材中的图片:http://sc.chinaz.com/tupian/gudianmeinvtupian.html import requests from lxml import ...

  3. CentOS6.7安装部署Nginx(详解主配置文件)

    keepalived + nginx   :实现高可用 nginx: web服务器 反向代理,也支持缓存(缓存在磁盘上) 支持FastCGI tengine:淘宝官方在nginx原有的代码的基础上对n ...

  4. Kali -关闭防火墙

    kali关闭防火墙前需要安装ufw 安装 apt-get install ufw 关闭 ufw disable # To disable the firewall 开启 ufw enable # To ...

  5. 接口自动化--读取Excel操作(openpyxl)

    上次我们已经将requests库封装成我们想要的样子了,我们的接口自动化已经完成了最开始的一步了,接下来我们需要完成我们相应的其他模块的封装,下面简单介绍下我们在接口自动化需要用到的模块吧在接口自动化 ...

  6. django项目中form表单和ajax的文件上传功能。

    form表单文件上传 路由 # from表单上传 path('formupload/',apply.formupload,name='formupload/'), 方法 # form表单文件上传 de ...

  7. 使用django的MTV开发模式返回一个网页

    1.MTV开发模式介绍 M:Models 模型(数据) 与数据组织相关的功能.组织和存储数据的方法和模式,与数据模型相关的操作. T:Templates 模板(样式) 与表现相关的所有功能.页面展示风 ...

  8. vim编辑时遇到E325: ATTENTION Found a swap file by the name "./.backu.sh.swp"错误代码的解决办法

    vim编辑时遇到E325: ATTENTION Found a swap file by the name "./.backu.sh.swp"错误代码的解决办法 重点:解决方法是: ...

  9. static在Swift 中表示 “类型范围作用域”

    In Swift, however, type properties are written as part of the type’s definition, within the type’s o ...

  10. Event 事件(最简单实用)

    public partial class Form1 : Form { /// <summary> /// 定义事件 /// </summary> public event E ...