设为首页 加入收藏

TOP

Ubuntu 16.04 下简单安装使用golang之备忘
2019-01-25 22:08:32 】 浏览:175
Tags:Ubuntu 16.04 简单 安装 使用 golang 备忘

??刚开始学习Go语言,这里记录下我在Ubuntu 16.04下安装使用golang的过程,方便以后查询。
?

一、安装

?

1、添加源

??如果使用默认的源安装golang的话,版本太低,只到1.6,所以要添加一个新的源并更新,执行以下命令:

sudo add-apt-repository ppa:longsleep/golang-backports
sudo apt update

?

2、安装

sudo apt install golang-go

??安装之后,可以通过go version命令查看版本。通过新的源安装下来,golang的版本是1.11.4(2019年1月25日)。
?

3、配置环境

??接下来,还需要做一点配置,我编写在~/.bashrc文件中,内容如下:

export GOPATH=/mydata/golang
export GOBIN=$GOPATH/bin
export PATH=$PATH:${GOPATH//://bin:}/bin

??GOROOT是Go的正式软件所在路径,在我们安装时系统已经自动设置好,所以不用在这里指明。GOPATH是我们自己的软件和第三方的Go软件所在路径,由自己指定,以后安装的各种附属工具会被安装在这里。
?

二、emacs中的golang支持配置

?

1、在自己设定的emacs插件目录中下载go-mode插件

??我习惯把emacs的插件目录放在目录:~/.emacs.d/plugins/。进入插件目录,执行以下克隆命令:

git clone https://github.com/dominikh/go-mode.el.git

?

2、编辑配置文件~/.emacs

??在.emacs文件中添加以下内容:

;; ---------------------golang---------------------
(add-to-list 'load-path "~/.emacs.d/plugins/go-mode.el")
(require 'go-mode)

??保存、退出,再进入编辑Go程序文件,就会有语法高亮、自动格式等功能。
?

三、代码格式

??Go的工具中提供了gofmt命令来格式化源代码文件,其命令参数如下:

usage: gofmt [flags] [path ...]
  -cpuprofile string
        write cpu profile to this file
  -d    display diffs instead of rewriting files
  -e    report all errors (not just the first 10 on different lines)
  -l    list files whose formatting differs from gofmt's
  -r string
        rewrite rule (e.g., 'a[b:len(a)] -> a[b:]')
  -s    simplify code
  -w    write result to (source) file instead of stdout

??如果是在emacs下编辑,并且已经安装上一步所提到的插件,那么格式会自动设置好。这个风格我不怎么喜欢,先用着吧。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Golint的简易使用方法 下一篇再探go modules:使用与细节

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目