Skip to content

Instantly share code, notes, and snippets.

@JeOam
Created September 21, 2016 02:21
Show Gist options
  • Save JeOam/1e338afed67c34dd60f8602a40ca866f to your computer and use it in GitHub Desktop.
Save JeOam/1e338afed67c34dd60f8602a40ca866f to your computer and use it in GitHub Desktop.
Go 入门笔记
@JeOam
Copy link
Author

JeOam commented Sep 21, 2016

  • $GOROOT 表示 Go 在你的电脑上的安装位置,它的值一般都是 $HOME/go,当然,你也可以安装在别的地方。
  • $GOARCH 表示目标机器的处理器架构,它的值可以是 386、amd64 或 arm。
  • $GOOS 表示目标机器的操作系统,它的值可以是 darwin、freebsd、linux 或 windows。
  • $GOBIN 表示编译器和链接器的安装位置,默认是 $GOROOT/bin,如果你使用的是 Go 1.0.3 及以后的版本,一般情况下你可以将它的值设置为空,Go 将会使用前面提到的默认值。
  • $GOPATH 默认采用和 $GOROOT 一样的值,但从 Go 1.1 版本开始,你必须修改为其它路径。它可以包含多个包含 Go 语言源码文件、包文件和可执行文件的路径,而这些路径下又必须分别包含三个规定的目录:src、pkg 和 bin,这三个目录分别用于存放源码文件、包文件和可执行文件。
  • $GOARM 专门针对基于 arm 架构的处理器,它的值可以是 5 或 6,默认为 6。
  • $GOMAXPROCS 用于设置应用程序可使用的处理器个数与核数。

@JeOam
Copy link
Author

JeOam commented Sep 21, 2016

Uninstalling Go:
To remove an existing Go installation from your system delete the go directory. This is usually /usr/local/go under Linux, Mac OS X.

You should also remove the Go bin directory from your PATH environment variable. Under Linux and FreeBSD you should edit /etc/profile or $HOME/.profile. If you installed Go with the Mac OS X package then you should remove the /etc/paths.d/go file.

Mac OS X package installer

Download the package file, open it, and follow the prompts to install the Go tools. The package installs the Go distribution to /usr/local/go.

The package should put the /usr/local/go/bin directory in your PATH environment variable. You may need to restart any open Terminal sessions for the change to take effect.

~/.zshrc 中添加:

export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin"  # 附加到原有 PATH 中
export GOPATH=$HOME/go

via Getting Started - The Go Programming Language

@JeOam
Copy link
Author

JeOam commented Sep 21, 2016

你可以按照下面的一些有用的方法来达到基本调试的目的:

  1. 在合适的位置使用打印语句输出相关变量的值(print/println 和 fmt.Print/fmt.Println/fmt.Printf)。
  2. 在 fmt.Printf 中使用下面的说明符来打印有关变量的相关信息:
    • %+v 打印包括字段在内的实例的完整信息
    • %#v 打印包括字段和限定类型名称在内的实例的完整信息
    • %T 打印某个类型的完整说明
  3. 使用 panic 语句来获取栈跟踪信息(直到 panic 时所有被调用函数的列表)。
  4. 使用关键字 defer 来跟踪代码执行过程。

@JeOam
Copy link
Author

JeOam commented Sep 23, 2016

new()make() 的区别:

  • new(T) 为每个新的类型T分配一片内存,初始化为 0 并且返回类型为*T的内存地址:这种方法 返回一个指向类型为 T,值为 0 的地址的指针,它适用于值类型如数组和结构体;它相当于 &T{}
  • make(T) 返回一个类型为 T 的初始值,它只适用于3种内建的引用类型:切片、map 和 channel。

换言之,new 函数分配内存,make 函数初始化;

@JeOam
Copy link
Author

JeOam commented Sep 24, 2016

有个被称作 Communicating Sequential Processes(顺序通信处理)(CSP, C. Hoare 发明的)还有一个叫做 message passing-model(消息传递)(已经运用在了其他语言中,比如 Erlang)。

@JeOam
Copy link
Author

JeOam commented Sep 26, 2016

What does an underscore in front of an import statement mean in Golang?

import _ "net/http/pprof"

This form of import makes clear that the package is being imported for its side effects, because there is no other possible use of the package: in this file, it doesn't have a name.

Doc: https://golang.org/doc/effective_go.html#blank_import

@JeOam
Copy link
Author

JeOam commented Sep 28, 2016

代码格式化当前目录所有 go 文件:

go fmt ./...

@JeOam
Copy link
Author

JeOam commented Oct 12, 2016

类型断言:

if v, ok := varI.(T); ok {  // checked type assertion
    Process(v)
    return
}
// varI is not of type T

如果转换合法,v 是 varI 转换到类型 T 的值,ok 会是 true;否则 v 是类型 T 的零值,ok 是 false,也没有运行时错误发生。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment