1. 匿名结构体

  • 全局组合
1
2
3
4
5
@H_502_29@var config struct {//定义一个用于全局配置结构体 @H_502_29@ APIKey string @H_502_29@ OAuthConfig oauth.Config @H_502_29@} @H_502_29@config.APIKey = "BADC0C0A" 

  • 数据模板
5
6
7
8
@H_502_29@data := //匿名结构体的定义 @H_502_29@ Title Users []*User @H_502_29@}{//同时初始化 @H_502_29@ title, @H_502_29@ users, @H_502_29@} @H_502_29@err := tmpl.Execute(w, data) 

(比map[string]interface{}消耗更小和更安全)

  • 测试表(用作测试数据构造)
8
9
10
@H_404_187@
@H_502_29@indexRuneTests = []struct { @H_502_29@ s rune rune @H_502_29@ out int @H_502_29@}{ @H_502_29@ {"a A x", 'A', 2}, @H_502_29@ {"some_text=some_value", '=',152)!important">9},152)!important">"☺a", 'a',152)!important">3},152)!important">"a☻☺b", '☺',152)!important">4}, @H_502_29@} 

  • 嵌套加锁
7
@H_502_29@hits sync.Mutex @H_502_29@ n int @H_502_29@} @H_502_29@hits.Lock() @H_502_29@n++//十对“n”的操作是安全的 @H_502_29@Unlock() 

2. 嵌套结构体

  • 反序列化深层嵌套的json
10
11
12
13
14
15
16
17
18
@H_404_187@
@H_502_29@{"data": {"children": [ @H_502_29@ {"data": { @H_502_29@ "title": "The Go homepage", @H_502_29@ "url": "http://golang.org/" @H_502_29@ }}, @H_502_29@ ... @H_502_29@]}} @H_502_29@type Item URL string @H_502_29@} @H_502_29@Response Data Children []Data Item @H_502_29@ } @H_502_29@ } @H_502_29@} 

3. godoc命令,输出package的文档注释

1
@H_502_29@% godoc sync Mutex 

输出

18
19
20
21
22
23
24
@H_404_187@
@H_502_29@PACKAGE @H_502_29@ @H_502_29@package sync @H_502_29@ import "sync" @H_502_29@ @H_502_29@TYPES @H_502_29@ @H_502_29@Mutex struct { @H_502_29@ // contains filtered or unexported fields @H_502_29@} @H_502_29@ A Mutex is a mutual exclusion lock. Mutexes can be created as part of @H_502_29@ other structures; the zero value for an unlocked mutex. @H_502_29@ @H_502_29@func (m *Mutex) Lock() @H_502_29@ Lock locks m. If lock already in use,210)!important">calling goroutine @H_502_29@ blocks until mutex available. @H_502_29@ @H_502_29@Unlock() @H_502_29@ Unlock unlocks It run-time error if m not locked on entry to @H_502_29@ Unlock. @H_502_29@ @H_502_29@ associated with particular goroutine. is @H_502_29@ allowed one goroutine to and then arrange another @H_502_29@ unlock it. 

4. godoc -src 命令

1
@H_502_29@% godoc -src sync Mutex 

输出

7
@H_502_29@// A Mutex is a mutual exclusion lock. @H_502_29@// Mutexes can be created as part of other structures; @H_502_29@// the zero value for a Mutex is an unlocked mutex. @H_502_29@state int32 @H_502_29@ sema uint32 @H_502_29@} 

未导出的元素也将显示!

5. 获取指定域名下的包

1
@H_502_29@go get camlistore.org/pkg/netutil 

go help remote可查看更详细的信息.

6. 模拟一个文件系统

获取到的包里,代码访问了文件系统,但是测试时不希望真正访问磁盘

20
@H_404_187@
@H_502_29@fs fileSystem = osFS{} @H_502_29@ @H_502_29@fileSystem interface { @H_502_29@ Open(name string) (file,210)!important">error) @H_502_29@ Stat(os.FileInfo,210)!important">error) @H_502_29@} @H_502_29@ @H_502_29@file io.Closer @H_502_29@ Reader @H_502_29@ ReaderAt @H_502_29@ Seeker @H_502_29@ Stat() (error) @H_502_29@} @H_502_29@ @H_502_29@// osFS 实现接口filesystem,并访问本地磁盘. @H_502_29@osFS struct{} @H_502_29@ @H_502_29@osFS) error) { return name) } @H_502_29@name) } 

7. 方法表达式

4
@H_502_29@T struct {} @H_502_29@T) Foo(string) { println(s) } @H_502_29@ @H_502_29@fn func(T, string) = T.Foo//将方法赋值给一个方法变量 

os/exec中的实际例子:

13
@H_404_187@
@H_502_29@c *Cmd) stdin() (f *File,210)!important">err error) @H_502_29@stdout() (stderr() (F func(*Cmd) (*_,210)!important">setupFd := range []F{(*Cmd).stdin, (*stdout,210)!important">stderr} { @H_502_29@ fd,210)!important">setupFd(c) @H_502_29@ err != nil { @H_502_29@ c.closeDescriptors(closeAfterStart) @H_502_29@ closeAfterWait) @H_502_29@ err @H_502_29@ } @H_502_29@ childFiles = append(childFiles,210)!important">fd) @H_502_29@} 

8. 使用统一个Channel发送和接收消息

22
@H_404_187@
@H_502_29@main @H_502_29@ @H_502_29@"fmt" @H_502_29@ @H_502_29@battle = make(chan string) @H_502_29@ @H_502_29@func warrior(string,210)!important">done chan struct{}) { @H_502_29@ select { @H_502_29@ case opponent := <-battle: @H_502_29@ fmt.Printf("%s beat %s\n",210)!important">name,210)!important">opponent) @H_502_29@ battle <- name: @H_502_29@ // I lost :-( @H_502_29@ } @H_502_29@ done <- struct{}{} @H_502_29@} @H_502_29@ @H_502_29@main() { @H_502_29@ done := struct{}) @H_502_29@ langs := []string{"Go", "C",152)!important">"C++",152)!important">"Java",152)!important">"Perl",152)!important">"Python"} @H_502_29@ l := range langs { go l,210)!important">done) } @H_502_29@ _ = langs { <-done } @H_502_29@} 

9. 使用channel的close发送广播

19
@H_404_187@
@H_502_29@waiter(i int,210)!important">block,22)!important">struct{}) { @H_502_29@ time.Sleep(Duration(rand.Intn(3000)) * Millisecond) @H_502_29@ Println(i,152)!important">"waiting...") @H_502_29@ <-block @H_502_29@ "done!") @H_502_29@ struct{}),22)!important">struct{}) @H_502_29@ i := 0; i < 4; i++ { @H_502_29@ done) @H_502_29@ } @H_502_29@ Sleep(5 * Second) @H_502_29@ close(block) @H_502_29@ i++ { @H_502_29@ <-done @H_502_29@ } @H_502_29@} 

另外一个例子

24 @H_404_187@
@H_502_29@worker(ch chan Work,210)!important">quit quitting bool @H_502_29@ for { @H_502_29@ w := <-ch: @H_502_29@ quitting { @H_502_29@ w.Refuse(); Println("worker",152)!important">"refused",210)!important">w) @H_502_29@ break @H_502_29@ } @H_502_29@ Do(); "processed",22)!important">case <-quit: @H_502_29@ "quitting") @H_502_29@ quitting = true @H_502_29@ } @H_502_29@ } @H_502_29@} @H_502_29@ch,210)!important">quit := Work),210)!important">makeWork(ch) @H_502_29@ i++ { quit) } @H_502_29@ quit) @H_502_29@ 2 * Second) @H_502_29@} 

10. select中使用空channel

quit == quit = nil @H_502_29@ } @H_502_29@ } @H_502_29@} @H_502_29@ @H_502_29@Second) @H_502_29@}