你的首个golang语言详细入门教程 | your first golang tutorial
2020-01-15 00:01
92 查看
本文首发于个人博客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
GOPATHenvironment variable specifies the location of your workspace. If noGOPATHis set, it is assumed to be$HOME/goon Unix systems.Note that
GOPATHmust 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 linksshould not be used to link files or directories into your workspace.An
import pathis a string that uniquely identifies a package.
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script><script>(adsbygoogle = window.adsbygoogle || []).push({});</script>
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
stringutilin
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.gothat contains functions named
TestXXXwith signature func
(t *testing.T). The test framework runs each such function; if the function calls a failure function such as
t.Erroror
t.Fail, the test is considered to have failed.
- file name: xxx_test.go
- function name: TextXXX
- error:
t.Error
ort.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
- Post author: kezunlin
- Post link: https://kezunlin.me/post/a0fb7f06/
- Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- Swift Tutorial(swift 教程) 4:Your First App
- Python入门教程 超详细1小时学会Python
- 十天学会易语言图解教程+快速入门
- GoLang语言环境搭建及idea集成开发(超详细)
- Yii PHP Framework实用入门教程(详细介绍)
- gulp详细入门教程
- gulp详细入门教程
- Golang 入门系列(三)Go语言基础知识汇总
- gulp详细入门教程
- Oracle数据库SQL语言实战培训教程(全面、详细、案例、精讲)套餐
- ThinkJS框架入门详细教程(一)开发环境
- XML详细入门教程[PDF]
- 这应该是2018年最详细的python爬虫入门教程了!
- WebPack详细入门教程(七)之css和sass的处理
- Python入门教程 超详细1小时学会Python
- 最新北风网Go语言重入门到实战视频教程 共31课
- C#语言初级入门(1)_C#教程
- ant使用指南详细入门教程
- Photoshop入门教程:文件菜单的详细讲解
- Go语言运行环境安装详细教程