本文首发于个人博客https://kezunlin.me/post/a0fb7f06/,欢迎阅读最新内容!

your first golang tutorial

go tutorial

versions:

  • go: 1.13.1

install

wget https://dl.google.com/go/go1.13.1.linux-amd64.tar.gz
sudo tar -C /usr/local -xzf go1.13.1.linux-amd64.tar.gz ll /usr/local/go vim ~/.bashrc
export PATH=$PATH:/usr/local/go/bin source ~/.bashrc

zsh uses env profile ~/.zshrc, bash use env profile ~/.bashrc.

check version

go version
go version go1.13.1 linux/amd64

uninstall

just delete /usr/local/go

set GOPATH

Create your workspace directory, $HOME/go.

The GOPATH environment variable specifies the location of your workspace. If no GOPATH is set, it is assumed to be $HOME/go on Unix systems.

Note that GOPATH must not be the same path as your Go installation.

issue the commands

vim .bashrc
# for golang
export GOPATH=$HOME/go
export PATH=/usr/local/go/bin:$GOPATH:$PATH source .bashrc #go env -w GOPATH=$HOME/go $ echo $GOPATH
/home/kezunlin/go $ go env GOPATH
/home/kezunlin/go

code organization

  • Go programmers typically keep all their Go code in a single workspace.
  • A workspace contains many version control repositories (managed by Git, for example).
  • Each repository contains one or more packages.
  • Each package consists of one or more Go source files in a single directory.
  • The path to a package's directory determines its import path.

like this

bin/
hello # command executable
outyet # command executable
src/
github.com/golang/example/
.git/ # Git repository metadata
hello/
hello.go # command source
outyet/
main.go # command source
main_test.go # test source
stringutil/
reverse.go # package source
reverse_test.go # test source
golang.org/x/image/
.git/ # Git repository metadata
bmp/
reader.go # package source
writer.go # package source
... (many more repositories and packages omitted) ...

Note that symbolic links should not be used to link files or directories into your workspace.

An import path is a string that uniquely identifies a package.

go example

your first program

mkdir -p $GOPATH/src/github.com/kezunlin/hello
cd $GOPATH/src/github.com/kezunlin/hello
vim hello.go

with code

package main

import "fmt"

func main() {
fmt.Printf("hello, world\n")
}

build and run

go build
./hello
hello, world

install and clean binary files

# install hello to $HOME/go/bin
go install # clean $HOME/go/bin/*
go clean -i

~/go/src$ go build github.com/kezunlin/hello/

~/go/src$ go install github.com/kezunlin/hello/

your first library

stringutil library

mkdir -p $GOPATH/src/github.com/kezunlin/stringutil
cd $GOPATH/src/github.com/kezunlin/stringutil
vim reverse.go

code

// Package stringutil contains utility functions for working with strings.
package stringutil // Reverse returns its argument string reversed rune-wise left to right.
func Reverse(s string) string {
r := []rune(s)
for i, j := 0, len(r)-1; i < len(r)/2; i, j = i+1, j-1 {
r[i], r[j] = r[j], r[i]
}
return string(r)
}
package name

where name is the package's default name for imports. (All files in a package must use the same name.)

executable commands must always use package main.

build library

go build github.com/kezunlin/stringutil
#This won't produce an output file. Instead it saves
#the compiled package in the local build cache.

use stringutil in hello.go

package main

import (
"fmt"
"github.com/kezunlin/stringutil"
) func main() {
fmt.Printf("hello, world\n")
fmt.Println(stringutil.Reverse("!oG ,olleH"))
}

build and install

go build github.com/kezunlin/hello
go install github.com/kezunlin/hello ~/go/bin$ ./hello
hello, world
Hello, Go!

folder structure

 tree .
.
├── bin
│   └── hello
└── src
└── github.com
└── kezunlin
├── hello
│   └── hello.go
└── stringutil
└── reverse.go 6 directories, 3 files

testing

You write a test by creating a file with a name ending in _test.go that contains functions named TestXXX with signature func (t *testing.T). The test framework runs each such function; if the function calls a failure function such as t.Error or t.Fail, the test is considered to have failed.

  • file name: xxx_test.go
  • function name: TextXXX
  • error: t.Error or t.Fail
package stringutil

import "testing"

func TestReverse(t *testing.T) {
cases := []struct {
in, want string
}{
{"Hello, world", "dlrow ,olleH"},
{"Hello, 世界", "界世 ,olleH"},
{"", ""},
}
for _, c := range cases {
got := Reverse(c.in)
if got != c.want {
t.Errorf("Reverse(%q) == %q, want %q", c.in, got, c.want)
}
}
}

test ok

 $ go test github.com/kezunlin/stringutil
ok github.com/kezunlin/stringutil 0.165s

test error

--- FAIL: TestReverse (0.00s)
reverse_test.go:16: Reverse("Hello, 世界2") == "2界世 ,olleH", want "界世 ,olleH"
FAIL
exit status 1
FAIL github.com/kezunlin/stringutil 0.003s

remote packages

$ go get github.com/golang/example/hello
$ $GOPATH/bin/hello
Hello, Go examples!

go commands

go help gopath
go help importpath
go help test go build
go install
go clean go get # fetch,build and install

Reference

History

  • 20191011: created.

Copyright

你的首个golang语言详细入门教程 | your first golang tutorial的更多相关文章

  1. ant使用指南详细入门教程

    这篇文章主要介绍了ant使用指南详细入门教程,本文详细的讲解了安装.验证安装.使用方法.使用实例.ant命令等内容,需要的朋友可以参考下 一.概述 ant 是一个将软件编译.测试.部署等步骤联系在一起 ...

  2. <转载>ant使用指南详细入门教程 http://www.jb51.net/article/67041.htm

    这篇文章主要介绍了ant使用指南详细入门教程,本文详细的讲解了安装.验证安装.使用方法.使用实例.ant命令等内容,需要的朋友可以参考下 一.概述 ant 是一个将软件编译.测试.部署等步骤联系在一起 ...

  3. gulp详细入门教程

    本文链接:http://www.ydcss.com/archives/18 gulp详细入门教程 简介: gulp是前端开发过程中对代码进行构建的工具,是自动化项目的构建利器:她不仅能对网站资源进行优 ...

  4. gulp详细入门教程(转载)

    本文转载自: gulp详细入门教程

  5. Docker最详细入门教程

    Docker原理.详细入门教程 https://blog.csdn.net/deng624796905/article/details/86493330 阮一峰Docker入门讲解 http://ww ...

  6. Kibana详细入门教程

    Kibana详细入门教程   目录 一.Kibana是什么 二.如何安装 三.如何加载自定义索引 四.如何搜索数据 五.如何切换中文 六.如何使用控制台 七.如何使用可视化 八.如何使用仪表盘 一.K ...

  7. Golang语言的入门开始

    一.golang介绍与安装 二.golang-hello world 三.golang的变量 四.golang的类型 五.golang的常量 六.golang的函数(func) 七.golang的包 ...

  8. go语言快速入门教程

    go快速入门指南 by 小强,2019-06-13 go语言是目前非常火热的语言,广泛应用于服务器端,云计算,kubernetes容器编排等领域.它是一种开源的编译型程序设计语言,支持并发.垃圾回收机 ...

  9. ActiveMQ详细入门教程系列(一)

    一.什么是消息中间件 两个系统或两个客户端之间进行消息传送,利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排队模型,它可以在分布式环境下 ...

随机推荐

  1. 小胖求学系列之-文档生成利器(下)-smart-doc

    叮叮叮....一阵铃声响起,大家都陆续来到了课堂,看老师没来,小张和小胖又闲聊起来,小张问:怎么样,smart-doc好用吧.小胖笑着说:挺好用的,不过? 小张看卖关子,问到:不过什么,有什么新发现? ...

  2. Django对DateTimeField时间字段查询QuerySet为空的解决方案

    今天在用的Django中的filter()方法对DateTimeField字段查询时碰到了问题,抓耳挠腮一下午,终于解决了,我觉得花了一下午的时间怎么着也得记录下吧(无语)...... 问题描述 : ...

  3. Java获取配置文件中的属性

    获取配置文件的属性值 example 目标配置文件jdbc.properties,现想要在java类里面调用opcl的url jdbc.url=jdbc:mysql://localhost:3306/ ...

  4. Java 判断密码是否是大小写字母、数字、特殊字符中的至少三种

    public class CheckPassword { //数字 public static final String REG_NUMBER = ".*\\d+.*"; //小写 ...

  5. 发布一个基于协程和事件循环的c++网络库

    目录 介绍 使用 性能 实现 日志库 协程 协程调度 定时器 Hook RPC实现 项目地址:https://github.com/gatsbyd/melon 介绍 开发服务端程序的一个基本任务是处理 ...

  6. Python活力练习Day5

    Day5:连续输入n个字符串,请按照长度为8拆分每个字符串后输出到新的字符串组:长度不是8的整数倍的字符串请在后面补数字0,其中,空字符串不做处理. eg : input :   2    12345 ...

  7. 从《彩色圆环》一题探讨一类环上dp的解法

    清橙A1202 bzoj2201 bsoj4074 试题来源 2010中国国家集训队命题答辩 问题描述 小A喜欢收集宝物.一天他得到了一个圆环,圆环上有N颗彩色宝石,闪闪发光.小A很爱惜这个圆环,天天 ...

  8. 搭建私人的云笔记_使用webdav服务

    搭建私人的云笔记_使用webdav服务 转载注明来源: 本文链接 来自osnosn的博客,写于 2019-10-10. 手机上有很多云笔记app,大多支持云存储.可是把笔记放在别人的服务器上,总觉得不 ...

  9. Css里的box-shadow的值分别代表什么

    以下为例: box-shadow:1px 2px 3px 4px color inset; 1px:表示沿x轴的正方向的长度(如果是负数,则为沿x轴的负方向的长度); 2px:表示沿y轴的正方向的长度 ...

  10. 本地的个人web网站上线的全过程,供大家参考(PHP,简易的LAMP环境搭建)

    一 : 你需要准备的东西 1.本地能访问的网站,最好是改过host文件和apache的httpd-vhosts.conf,配置过本地域名的那种(减少传到线上出现的问题,文件路径不对呀啥的) 2.一个云 ...