Go语言的“++”和“—”运算符

以下摘自The Go Programming Language

The increment statement i++ adds 1 to i ; it’s equivalent to i += 1 which is in turn equivalent to i = i + 1 . There’s a corresponding decrement statement i– that subtracts 1. These are statements, not expressions as the y are in most languages in the C family, so j = i++ is illegal, and the y are postfix only, so –i is not legal either.

要注意,i++i--Go语言中是语句,不是表达式,因此不能赋值给另外的变量。此外没有++i--i

 

Go语言import语句的位置

以下摘自The Go Programming Language

The import declarations must follow the package declaration.After that, a program consists of the declarations of functions, variables, constants, and types (introduced by the key words func, var , const , and type ); for the most part, the order of declarations does not matter.

import语句必须跟在package定义后面。请看下例:

package main

var str string = "Hello world!\n"

import "fmt"

func main(){
    // your code goes 
    fmt.Println(str)
}

编译如下:

./prog.go:5: syntax error: unexpected import

调整一下语句位置:

package main

import "fmt"

var str string = "Hello world!\n"

func main(){
    // your code goes 
    fmt.Println(str)
}

编译执行成功:

Hello world!

 

Go语言的“main package”

以下摘自The Go Programming Language

Package main is special. It defines a standalone executable program, not a library. Within package main the function main is also special—it’s where execution of the program begins. Whatever main does is what the program does.

main package不同于其它library package,它定义了一个可执行程序。其中的main函数即是可执行文件的入口函数。

 

Kubernetes笔记(2)—— 编译时的workspace

编译k8s代码时,会在k8s根目录下生成一个_output文件夹,同时这个文件夹下还包含local文件夹:

~/kubernetes/_output/local$ ls
bin  go

go文件夹下就是一个标准的Go语言workspace

:~/kubernetes/_output/local/go$ ls -alt
total 20
drwxrwxr-x 4 nan nan 4096 Dec  9 22:09 ..
drwxrwxr-x 2 nan nan 4096 Dec  9 22:09 bin
drwxrwxr-x 4 nan nan 4096 Dec  9 22:08 pkg
drwxrwxr-x 5 nan nan 4096 Dec  9 22:07 .
drwxrwxr-x 3 nan nan 4096 Dec  9 22:04 src

进入src文件夹:

~/kubernetes/_output/local/go/src$ ls -alt
total 12
drwxrwxr-x 5 nan nan 4096 Dec  9 22:07 ..
drwxrwxr-x 2 nan nan 4096 Dec  9 22:06 k8s.io
drwxrwxr-x 3 nan nan 4096 Dec  9 22:04 .
nan@ubuntu:~/kubernetes/_output/local/go/src$ cd k8s.io/
nan@ubuntu:~/kubernetes/_output/local/go/src/k8s.io$ ls -alt
total 8
drwxrwxr-x 2 nan nan 4096 Dec  9 22:06 .
lrwxrwxrwx 1 nan nan   20 Dec  9 22:06 kubernetes -> /home/nan/kubernetes
drwxrwxr-x 3 nan nan 4096 Dec  9 22:04 ..

可以看到,src/k8s.io/kubernetes就是一个指向外层工作目录的软链接。

至此,可以理解代码里下面import语句为什么能工作了:

import (
    "k8s.io/kubernetes/contrib/mesos/pkg/controllermanager"
    "k8s.io/kubernetes/contrib/mesos/pkg/hyperkube"
)

 

Go语言的workspace

一个典型的Golang workspace包含3个文件夹:

src contains Go source files organized into packages (one package per directory),  
pkg contains package objects, and  
bin contains executable commands.

src包含这个工程的所有源码文件(包含依赖的第三方package的源码),pkg包含编译生成的package目标文件,而bin包含最后生成的可执行文件。下面是一个例子:

bin/
    hello                          # command executable
    outyet                         # command executable
pkg/
    linux_amd64/
        github.com/golang/example/
            stringutil.a           # package object
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  

参考资料:
Workspaces

 

Go语言实践技巧(8)——channel类型

声明channel时,<-表明方向:

chan T          // 能收发`T`类型变量
chan<- float64  // 只能发送 float64 类型变量 (write-only)
<-chan int      // 只能接收 int 类型变量 (read-only)

<-同最左边的channel结合:

chan<- chan int    // 同 chan<- (chan int)
chan<- <-chan int  // 同 chan<- (<-chan int)
<-chan <-chan int  // 同 <-chan (<-chan int)

参考资料:
How to understand “<-chan” in declaration?

Go语言实践技巧(7)——value receiver和pointer receiver

Value receiver:

func (u user) fun1() {
    ....
}

Pointer receiver:

func (u *user) fun2() {
    ....
}

Value receiver操作的是值的拷贝,而pointer receiver操作的是实际的值。

pointer去调用value receiver的方法,实际的操作是:

(*p).fun1()

而用value去调用pointer receiver的方法,实际的操作是:

(&v).fun2()

参考资料:
Go in Action

Go语言实践技巧(6)——map key的选择

The map key can be a value from any built-in or struct type as long as the value can be used in an expression with the == operator. Slices, functions, and struct types that contain slices can’t be used as a map key.

 

map key可以使用任何内置类型或结构类型的值,只要这个值可以使用在==表达式中。slice,函数,和包含slice的结构体不能用作key

参考资料:
Go in Action

Go语言实践技巧(4)——goroutine之间切换的时间点

以下列出了goroutine之间切换的主要的时间点:

(1)Channel发送和接收操作(如果这些操作是阻塞的);
(2)执行go语句,虽然不能保证新的goroutine马上被调度执行;
(3)阻塞的系统调用,像文件操作,网络操作等等;
(4)停下来进入垃圾回收周期以后。

换句话讲,在goroutine不能继续进行运算以后(需要更多数据,更多空间,等等),都会进行切换。

参考资料:
Performance without the event loop