| 
 | 1 | +# golang tips  | 
 | 2 | + | 
 | 3 | + | 
 | 4 | +## how to write golang  | 
 | 5 | + | 
 | 6 | +[offical how-to-write-go-code](https://golang.org/doc/code.html)  | 
 | 7 | + | 
 | 8 | +### all code in workspace, which different from other language  | 
 | 9 | + | 
 | 10 | + | 
 | 11 | +### import_path and package_name  | 
 | 12 | + | 
 | 13 | +1) import_path need to be unique  | 
 | 14 | + | 
 | 15 | +2) import_path is not package_name  | 
 | 16 | + | 
 | 17 | +3) for convenience, `import_path == vcs_base_url<github.com/user>/ + package_name``, but this is not neccesary  | 
 | 18 | + | 
 | 19 | +4) execute_program package name must be main  | 
 | 20 | + | 
 | 21 | +5) all file in same import path must have same package_name  | 
 | 22 | + | 
 | 23 | + | 
 | 24 | +## effective go  | 
 | 25 | + | 
 | 26 | +[effective go](https://golang.org/doc/effective_go.html#names)  | 
 | 27 | + | 
 | 28 | +### go examples  | 
 | 29 | + | 
 | 30 | +### format  | 
 | 31 | +gofmt auto do this.  | 
 | 32 | + | 
 | 33 | +use indentation instead of space  | 
 | 34 | + | 
 | 35 | +line length is at you wish.  | 
 | 36 | + | 
 | 37 | +### comment and doc  | 
 | 38 | + | 
 | 39 | +package_doc is before `package name`,  | 
 | 40 | +in many file package should only one-file define it.  | 
 | 41 | +it usually define in doc.go.  | 
 | 42 | + | 
 | 43 | +it should only be text, html is not good idea.  | 
 | 44 | + | 
 | 45 | + | 
 | 46 | +func/variable doc is comment immediately preceding a top-level declaration.  | 
 | 47 | +Every exported (capitalized) name in a program should have a doc comment.  | 
 | 48 | + | 
 | 49 | +### names  | 
 | 50 | + | 
 | 51 | +1) package name should be good: short, concise, evocative  | 
 | 52 | + | 
 | 53 | +2) packages are given lower case, single-word names; there should be no need for underscores or mixedCaps.  | 
 | 54 | + | 
 | 55 | +3) Getter -> <Owner>(Captial). setter -> (SetOwner)(with Set prefix)  | 
 | 56 | + | 
 | 57 | +4)  one-method interfaces are named by the method name plus an -er(Ex: Reader, Writer, Formatter)  | 
 | 58 | + | 
 | 59 | +5) Read/Write/String and so on not use in your func name.   | 
 | 60 | + | 
 | 61 | +6) use MixCaps instead of underscore for multiword-names.  | 
 | 62 | + | 
 | 63 | +7) Semicolons is not required.  | 
 | 64 | + | 
 | 65 | +# control stucture  | 
 | 66 | + | 
 | 67 | +1) if/for support short declare  | 
 | 68 | + | 
 | 69 | +2) for is for/while/foreach  | 
 | 70 | + | 
 | 71 | +3) foreach => for i, v := range items  | 
 | 72 | + | 
 | 73 | +4) switch and fallthrough  | 
 | 74 | + | 
 | 75 | +5) type_switch  | 
 | 76 | +```golang  | 
 | 77 | +switch t := t.(type) {  | 
 | 78 | +    case bool: ...  | 
 | 79 | +    case int: ...  | 
 | 80 | +}  | 
 | 81 | +```  | 
 | 82 | + | 
 | 83 | + | 
 | 84 | +## the go programming spec  | 
 | 85 | + | 
 | 86 | +[offical go spec](https://golang.org/ref/spec)  | 
0 commit comments