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. windows jupyter notebook 切换默认环境

    windows jupyter notebook 切换默认环境 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog ...

  2. 【robotframework】pycharm+robotframe(转)

    [robotframework]pycharm+robotframe   一.环境搭建 二.框架介绍 1.settings 是这个测试套件的全局配置表 说明这个测试套件要使用的测试库.资源文件 测试套 ...

  3. error C4996: 'AVStream::codec': was declared deprecated

    关闭VS的SDL检查 工程 属性=>C/C++ =>General=> SDL checks 改为 No(/sdl).

  4. Docker搭建Redis

    1.拉取redis镜像: docker pull redis 2.创建容器: docker run -d --restart=always -v /opt/redis/data:/data --nam ...

  5. js 压缩图片 上传

    感谢,参考了以下作者的绝大部分内容 https://blog.csdn.net/tangxiujiang/article/details/78755292 https://blog.csdn.net/ ...

  6. python爬取站长之家植物图片

    from lxml import etree from urllib import request import urllib.parse import time import os def hand ...

  7. SaltStack--项目实战

    saltstack项目实战 项目架构规划 后端web服务器使用Nginx+Php作为站点,通过HAproxy做负载均衡,Keepalived做高可用 项目环境准备 说明: 关闭防火墙.selinux. ...

  8. mysql分析sql语句基础工具 -- explain

    分析sql语句 explain explain (sql语句) G; 分析结果: id sql语句编号如果是连接查询,表之间是平等关系,编号相同:如果有子查询,编号递增. select——type 查 ...

  9. python中pop()与split()的用法

    imglist = ['11.jpg','12.jpg','13.jpg','14.jpg','2.jpg','1.jpg',] print(str(imglist)) a = str(imglist ...

  10. 使用ArcGIS for Server的Feature Access REST在线编辑图层

    如何启用Feature Access可以参考以前写的一篇博客:http://www.cnblogs.com/oceanking/p/3895257.html 本文主要关注一个全是点的图层,我也不知道学 ...