set fileencodings=utf-8,cp936 " 打开文件编码按顺序匹配
set nobomb "
+
+"为不同的文件类型设置不同的空格数替换TAB
+"autocmd FileType php,python,c,java,perl,shell,bash,vim,ruby,cpp set ai
+"autocmd FileType php,python,c,java,perl,shell,bash,vim,ruby,cpp set sw=4
+"autocmd FileType php,python,c,java,perl,shell,bash,vim,ruby,cpp set ts=4
+"autocmd FileType php,python,c,java,perl,shell,bash,vim,ruby,cpp set sts=4
+
+"golang默认使用TAB
+autocmd FileType go set ai
+autocmd FileType go set tabstop=4
+autocmd FileType go set shiftwidth=4
+autocmd FileType go set noexpandtab
+
imap jj <esc> " 连续按两次jj表示按ESC,进入命令模式
import "fmt"
type Vector struct {
- x, y uint32
+ x, y uint32
}
-
// map在使用前必须使用make而不是new来创建,值为nil的map是空的,并且不能赋值
var Map map[string]Vector
func main() {
- // [n]T 表示array
+ // [n]T 表示array
var array [2]string
array[0] = "HELLO"
array[1] = "GO"
fmt.Println(b)
fmt.Println(c)
-
// SLICE
- // []T 表示slice
- var slice []string
- fmt.Println("Slice len", len(slice), "cap", cap(slice))
- // slice 的零值是 `nil`。
- if slice == nil {
- fmt.Println("Slice is nil")
- }
- slice = array[:]
- fmt.Println(slice)
- fmt.Println("Slice len", len(slice), "cap", cap(slice))
-
- // 清空slice
- slice = nil
- fmt.Println("Slice len", len(slice), "cap", cap(slice))
-
- // 构造slice
- S := make([]int, 5/*length*/, 10/*cap*/) // cap >= length
- fmt.Println("Slice s len", len(S), "cap", cap(S))
+ // []T 表示slice
+ var slice []string
+ fmt.Println("Slice len", len(slice), "cap", cap(slice))
+ // slice 的零值是 `nil`。
+ if slice == nil {
+ fmt.Println("Slice is nil")
+ }
+ slice = array[:]
+ fmt.Println(slice)
+ fmt.Println("Slice len", len(slice), "cap", cap(slice))
+
+ // 清空slice
+ slice = nil
+ fmt.Println("Slice len", len(slice), "cap", cap(slice))
+
+ // 构造slice
+ S := make([]int, 5 /*length*/, 10 /*cap*/) // cap >= length
+ fmt.Println("Slice s len", len(S), "cap", cap(S))
d := []byte{'a', 'b', 'c'}
fmt.Println(d)
var e []string = []string{"a", "b", "c"}
- // 向slice中添加元素
- e = append(e, "append:d")
- e = append(e, "append:e", "append:f")
+ // 向slice中添加元素
+ e = append(e, "append:d")
+ e = append(e, "append:e", "append:f")
for i := 0; i < len(e); i++ {
fmt.Println(e[i])
}
- // range
+ // range
for i, v := range d {
fmt.Println(i, v)
}
- // 如果只需要索引值,去掉`value`部分就可以了
- for i := range(e) {
- fmt.Println(i)
- }
-
- // 如果只需要`value`部分,索引部分可以直接用`_`来忽略
- for _, v := range(e) {
- fmt.Println(v)
- }
-
-
- if Map == nil {
- fmt.Println("Map is nil")
- }
- Map = make(map[string]Vector)
- if Map == nil {
- fmt.Println("Map is nil")
- } else {
- fmt.Println("Map is not nil")
- }
- Map["LeftTop"] = Vector{0, ^uint32(0)}
- Map["LeftBottom"] = Vector{0, 0}
- Map["RightBottom"] = Vector{^uint32(0), 0}
- Map["RightTop"] = Vector{^uint32(0), ^uint32(0)}
-
- fmt.Println(Map)
- fmt.Println(Map["RightTop"])
- fmt.Println(Map["RightTops"])
-
-
- var MapA=map[string]string {
- "Apple" : "USA",
- "Google" : "USA",
- "Tencent" : "CHN",
- }
-
- fmt.Println(MapA)
-
- // insert
- MapA["Test"] = "Unknown"
- fmt.Println(MapA)
-
- // modify
- MapA["Test"] = "USA"
- fmt.Println(MapA)
-
- var s string
- var ok bool
- s, ok = MapA["Test"]
- if !ok {
- fmt.Printf("Key:Test not in\n")
- } else {
- fmt.Printf("Key:Test = %s\n", s)
- }
-
- // delete
- delete(MapA, "Test")
- fmt.Println(MapA)
-
- _, ok = MapA["Test"]
- if !ok {
- fmt.Println("Key:Test not in")
- }
+ // 如果只需要索引值,去掉`value`部分就可以了
+ for i := range e {
+ fmt.Println(i)
+ }
+
+ // 如果只需要`value`部分,索引部分可以直接用`_`来忽略
+ for _, v := range e {
+ fmt.Println(v)
+ }
+
+ if Map == nil {
+ fmt.Println("Map is nil")
+ }
+ Map = make(map[string]Vector)
+ if Map == nil {
+ fmt.Println("Map is nil")
+ } else {
+ fmt.Println("Map is not nil")
+ }
+ Map["LeftTop"] = Vector{0, ^uint32(0)}
+ Map["LeftBottom"] = Vector{0, 0}
+ Map["RightBottom"] = Vector{^uint32(0), 0}
+ Map["RightTop"] = Vector{^uint32(0), ^uint32(0)}
+
+ fmt.Println(Map)
+ fmt.Println(Map["RightTop"])
+ fmt.Println(Map["RightTops"])
+
+ var MapA = map[string]string{
+ "Apple": "USA",
+ "Google": "USA",
+ "Tencent": "CHN",
+ }
+
+ fmt.Println(MapA)
+
+ // insert
+ MapA["Test"] = "Unknown"
+ fmt.Println(MapA)
+
+ // modify
+ MapA["Test"] = "USA"
+ fmt.Println(MapA)
+
+ var s string
+ var ok bool
+ s, ok = MapA["Test"]
+ if !ok {
+ fmt.Printf("Key:Test not in\n")
+ } else {
+ fmt.Printf("Key:Test = %s\n", s)
+ }
+
+ // delete
+ delete(MapA, "Test")
+ fmt.Println(MapA)
+
+ _, ok = MapA["Test"]
+ if !ok {
+ fmt.Println("Key:Test not in")
+ }
}
* Description: none
* ------------------------------------------------------------------------
*/
- package main
+package main
- import "fmt"
- import "time"
- import "math/rand"
+import "fmt"
+import "time"
+import "math/rand"
- func main() {
+func main() {
- rand.Seed(time.Now().UnixNano())
+ rand.Seed(time.Now().UnixNano())
- // basic for
- sum := 0
- for i:=0; i<10; i++ {
- sum += i
- }
+ // basic for
+ sum := 0
+ for i := 0; i < 10; i++ {
+ sum += i
+ }
- fmt.Println("Sum:", sum)
+ fmt.Println("Sum:", sum)
+ // 与C语言一样 go 的for 前置、后置条件可以为空
+ sum = 1
+ for sum <= 1000 {
+ sum += sum
+ }
- // 与C语言一样 go 的for 前置、后置条件可以为空
- sum = 1
- for ; sum <= 1000; {
- sum += sum
- }
+ fmt.Println("Sum:", sum)
- fmt.Println("Sum:", sum)
+ // for 也是go的 'while'
+ for sum >= 0 {
+ sum -= rand.Intn(100)
+ }
- // for 也是go的 'while'
- for sum >= 0 {
- sum -= rand.Intn(100)
- }
+ fmt.Println("Sum:", sum)
- fmt.Println("Sum:", sum)
-
- // 死循环
- // for {
- // }
- }
+ // 死循环
+ // for {
+ // }
+}
--- /dev/null
+/*
+ * ------------------------------------------------------------------------
+ * File Name: func.go
+ * Author: Zhao Yanbai
+ * 2015-11-14 15:51:24 Saturday CST
+ * Description: none
+ * ------------------------------------------------------------------------
+ */
+package main
+
+import (
+ "fmt"
+ "math"
+ "math/rand"
+ "time"
+)
+
+
+func main() {
+ defer fmt.Println("Program Exited...")
+
+ // 函数也是值
+ hypot := func(x, y float64) float64 {
+ return math.Sqrt(x*x + y*y)
+ }
+
+ rand.Seed(time.Now().UnixNano())
+ fmt.Println(hypot(rand.Float64(), rand.Float64()))
+
+ // 闭包
+
+ // 两个闭包实例
+ pos := Adder()
+ neg := Adder()
+
+ // NOTICE 十次是两个实例的累计结果,不是独立函数调用结果
+ for i:=0; i<10; i++ {
+ fmt.Printf("Positive: %5d Negative: %5d\n", pos(i), neg(-i*i))
+ }
+
+
+ // Fibonacci
+ fibonacci := Fibonacci()
+ for i:=0; i<10; i++ {
+ fmt.Printf("%02d - %5d\n", i, fibonacci())
+ }
+}
+
+func Adder() func(int) int {
+
+ sum := 0
+
+ // go语言不能在函数内部显示嵌套定义函数
+ // 但是可以定义一个匿名函数
+ innerfunc := func(x int) int {
+ sum += x
+ return sum
+ }
+
+ return innerfunc
+}
+
+
+func Fibonacci() func() int {
+ a := 0
+ b := 1
+ // a, b := 0, 1
+ innerfunc := func() int {
+ t := b
+ b = a+b
+ a = t
+ //a, b = b, a+b
+ return a
+ }
+
+ return innerfunc
+}
import "time"
func GetStr() (string, string) {
- // 常量可以是数字、字符串、布尔或字符
+ // 常量可以是数字、字符串、布尔或字符
const stra = "Hello"
const strb string = "World"
return stra, strb
// 多常量
const (
- BigInt = 1 << 9
- ConstStr= "HELLO WORLD"
+ BigInt = 1 << 9
+ ConstStr = "HELLO WORLD"
)
func PrintType() {
fmt.Printf("%T %T %T %T %T %T %T %T\n", a, b, c, d, e, f, g, h)
}
-
func Add(a int, b int) int {
- return a+b
+ return a + b
}
-
// 当两个或多个连续参数是同一类型,除了最后一个类型之外,其它的都可以省略
func SwapString(a, b string) (string, string) {
- return b, a
+ return b, a
}
-
// 返回值可以被命名
func Split(sum int) (a, b int) {
- a = sum * 4 / 9
- b = sum - a
- return
+ a = sum * 4 / 9
+ b = sum - a
+ return
}
-
// 定义变量
var va, vb, vc string
var fa float64 = 2.71828182846
var xa, xb, xc = true, "xb", 0xDD
-
func main() {
- // defer 的参数会立刻生成,但是只是在程序结束时调用
+ // defer 的参数会立刻生成,但是只是在程序结束时调用
defer fmt.Println("----------------")
rand.Seed(time.Now().UnixNano())
- n := 0
+ n := 0
for i := 0; i < rand.Intn(10)+1; i++ {
fmt.Println(GetStr())
- n = i
+ n = i
}
- // defer 会逆序调用
- defer fmt.Println("n:", n)
+ // defer 会逆序调用
+ defer fmt.Println("n:", n)
PrintType()
- fmt.Println(Add(3, 543))
-
- fmt.Println(SwapString("Hello", "World"))
-
- fmt.Println(Split(342))
-
-
- va = "va"
- vb = "vb"
- vc = "vc"
- fmt.Println(va, vb, vc)
+ fmt.Println(Add(3, 543))
- fmt.Println(ia, ib, ic)
+ fmt.Println(SwapString("Hello", "World"))
- fmt.Println(fa)
+ fmt.Println(Split(342))
- fmt.Println(xa, xb, xc)
+ va = "va"
+ vb = "vb"
+ vc = "vc"
+ fmt.Println(va, vb, vc)
+ fmt.Println(ia, ib, ic)
- // 短声明,只能用在函数内部
- sa := 34
- // 类型推导
- sb := sa
- fmt.Println(sa, sb)
+ fmt.Println(fa)
- // 强制类型转换
- sc := float64(sa)
- fmt.Println(sc)
+ fmt.Println(xa, xb, xc)
- // 常量不能用:=语法定义
- // const sd := sa
+ // 短声明,只能用在函数内部
+ sa := 34
+ // 类型推导
+ sb := sa
+ fmt.Println(sa, sb)
- fmt.Println(BigInt, ConstStr)
+ // 强制类型转换
+ sc := float64(sa)
+ fmt.Println(sc)
+ // 常量不能用:=语法定义
+ // const sd := sa
+ fmt.Println(BigInt, ConstStr)
- // 指针, go 语言没有指针运算
- var pi *int
- pj := &n
- pi = pj
+ // 指针, go 语言没有指针运算
+ var pi *int
+ pj := &n
+ pi = pj
- *pj = 123
+ *pj = 123
- fmt.Println(n, *pi, *pj, pi, pj)
+ fmt.Println(n, *pi, *pj, pi, pj)
}
* ------------------------------------------------------------------------
*/
package main
+
import (
- "fmt"
- "time"
- "math/rand"
+ "fmt"
+ "math/rand"
+ "time"
)
func main() {
- rand.Seed(time.Now().UnixNano())
-
- n := rand.Int()
- if n % 2 == 0 {
- fmt.Println("Even:", n)
- } else {
- fmt.Println("Odd:", n)
- }
+ rand.Seed(time.Now().UnixNano())
+ n := rand.Int()
+ if n%2 == 0 {
+ fmt.Println("Even:", n)
+ } else {
+ fmt.Println("Odd:", n)
+ }
- // if 语句在条件之前可以执行一条简单的代码
- if v := rand.Int(); v % 3 == 0 {
- fmt.Println(v, " MOD 3 == 0")
- } else if v % 3 == 1 {
- fmt.Println(v, " MOD 3 == 1")
- } else {
- fmt.Println(v, " MOD 3 == 2")
- }
+ // if 语句在条件之前可以执行一条简单的代码
+ if v := rand.Int(); v%3 == 0 {
+ fmt.Println(v, " MOD 3 == 0")
+ } else if v%3 == 1 {
+ fmt.Println(v, " MOD 3 == 1")
+ } else {
+ fmt.Println(v, " MOD 3 == 2")
+ }
}
--- /dev/null
+/*
+ * ------------------------------------------------------------------------
+ * File Name: interfaces.go
+ * Author: Zhao Yanbai
+ * 2015-11-14 17:47:59 Saturday CST
+ * Description: none
+ * ------------------------------------------------------------------------
+ */
+package main
+import (
+ "fmt"
+)
+
+func main() {
+ defer fmt.Println("Program Exited...")
+
+}
--- /dev/null
+/*
+ * ------------------------------------------------------------------------
+ * File Name: methods.go
+ * Author: Zhao Yanbai
+ * 2015-11-14 17:22:24 Saturday CST
+ * Description: none
+ * ------------------------------------------------------------------------
+ */
+package main
+import (
+ "fmt"
+ "math"
+ "time"
+ "math/rand"
+)
+
+type Vector struct {
+ x, y float64
+}
+
+
+// 不用指针型也可以
+// 此处于C语言同理,用指针能改变则用指针拷贝
+// 用值则用值拷贝,指针一般更有效率,可以修改
+// 指向的值
+func (v *Vector) Abs() float64 {
+ return math.Sqrt(v.x*v.x + v.y*v.y)
+}
+
+// 可以将v换成非指针型对比
+func (v *Vector) Scale(scale float64) {
+ v.x = v.x*scale
+ v.y = v.y*scale
+}
+
+// fmt中的一个接口
+// type Stringer interface {
+// String() string
+// }
+// 不能是指针
+func (v Vector) String() string {
+ return fmt.Sprintf("Vector{x:%.4f, y:%.4f}", v.x, v.y)
+}
+
+
+// 根据现有类型定义专用类型
+type FloatType float64;
+
+func (f FloatType) TenTimesInt() int {
+ return int(f*10)
+}
+
+func main() {
+ defer fmt.Println("Program Exited...")
+
+ rand.Seed(time.Now().UnixNano())
+
+ v1 := Vector{rand.Float64(), rand.Float64()}
+ var v2 Vector = Vector{rand.Float64(), rand.Float64()}
+
+ fmt.Println(v1)
+ fmt.Println(v2)
+
+ v1.Scale(10.0)
+ v2.Scale(0.9)
+
+ fmt.Println(v1)
+ fmt.Println(v2)
+
+ fmt.Println(v1.Abs())
+ fmt.Println(v2.Abs())
+
+
+ f := FloatType(rand.Float64())
+ fmt.Printf("Float: %7.4f Ten Times Integer: %2d\n", f, f.TenTimesInt())
+}
}
func main() {
- rand.Seed(time.Now().UnixNano())
+ rand.Seed(time.Now().UnixNano())
v := Vertex{1, 2}
fmt.Println(v)
- fmt.Println(Vertex{0xABC, 0xCBA})
+ fmt.Println(Vertex{0xABC, 0xCBA})
- // 通过指针间隔访问的过程是透明的
+ // 通过指针间隔访问的过程是透明的
p := &v
p.X = rand.Int()
- p.Y = rand.Int()
+ p.Y = rand.Int()
fmt.Println(v)
- v.X, v.Y = p.Y, p.X
- fmt.Println(v.X, v.Y)
+ v.X, v.Y = p.Y, p.X
+ fmt.Println(v.X, v.Y)
}
import (
"fmt"
+ "math/rand"
"runtime"
"time"
- "math/rand"
)
func main() {
- rand.Seed(time.Now().UnixNano())
+ rand.Seed(time.Now().UnixNano())
fmt.Print("Go runs on ")
switch os := runtime.GOOS; os {
case "darwin":
fmt.Printf("%s.", os)
}
- // switch 可以没有表达示,将变为一个更清晰的if-then-else的代码结构
+ // switch 可以没有表达示,将变为一个更清晰的if-then-else的代码结构
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("Good evening.")
}
- //fallthrough
- switch v:=rand.Intn(4); v {
- case 0:
- fmt.Println("Level0")
- fallthrough
- case 1:
- fmt.Println("Level1")
- fallthrough
- case 2:
- fmt.Println("Level2")
- fallthrough
- case 3:
- fmt.Println("Level3")
- fallthrough
- default:
- fmt.Println("Ground")
- }
-
+ //fallthrough
+ switch v := rand.Intn(4); v {
+ case 0:
+ fmt.Println("Level0")
+ fallthrough
+ case 1:
+ fmt.Println("Level1")
+ fallthrough
+ case 2:
+ fmt.Println("Level2")
+ fallthrough
+ case 3:
+ fmt.Println("Level3")
+ fallthrough
+ default:
+ fmt.Println("Ground")
+ }
}
$f =~ s/\./_/g;
$f = "H_" . uc($f) . "__";
$as .= "\n#pragma once\n";
+ } elsif($index eq "go") {
+ $as .= "package main\n";
+ $as .= "import (\n";
+ $as .= " \"fmt\"\n";
+ $as .= ")\n\n";
+ $as .= "func main() {\n";
+ $as .= " defer fmt.Println(\"Program Exited...\")\n\n";
+ $as .= "}";
+ $as = "" unless($create_main);
} elsif($index eq "py") {
} elsif($index eq "rb") {
} elsif($index eq "pl") {