From: acevest Date: Sat, 28 Nov 2015 08:09:00 +0000 (+0800) Subject: learn go... X-Git-Url: http://zhaoyanbai.com/repos/Bv9ARM.ch12.html?a=commitdiff_plain;h=7c3922f89543b21522178b102445c2bb2b8c9e64;p=acecode.git learn go... --- diff --git a/learn/doc/mac_bash_profile b/learn/doc/mac_bash_profile index 1b0d2b6..3d234dd 100644 --- a/learn/doc/mac_bash_profile +++ b/learn/doc/mac_bash_profile @@ -5,8 +5,11 @@ HISTCONTROL=ignoreboth export PATH=/usr/local/sbin:$PATH export PATH="$(brew --prefix homebrew/php/php56)/bin:$PATH" export PATH=/Users/Ace/.local/bin:$PATH +export GOBIN=/Users/Ace/.local/bin export PATH=/Users/Ace/workspace/github/metasploit-framework:$PATH export MSF_DATABASE_CONFIG=/Users/Ace/workspace/github/metasploit-framework/config/database.yml +#多个GOPATH用';'分隔 go get会存到第一个目录 +export GOPATH=/Users/Ace/workspace/go export PS1="\W\$ " alias ll='ls -lGh' alias ls='ls -Gh' diff --git a/learn/doc/vimrc b/learn/doc/vimrc index 102abf1..a298c7e 100644 --- a/learn/doc/vimrc +++ b/learn/doc/vimrc @@ -72,6 +72,8 @@ set tabstop=4 " TAB的宽度 set shiftwidth=4 set expandtab " TAB用空格代替,否则可用 set noexpandtab set cursorline +set paste + "hi CursorLine ctermbg=lightcyan cterm=BOLD "set cursorcolumn "hi CursorColumn ctermbg=lightgreen cterm=BOLD diff --git a/learn/go/for.go b/learn/go/for.go index 9516270..641af7f 100644 --- a/learn/go/for.go +++ b/learn/go/for.go @@ -42,4 +42,21 @@ func main() { // 死循环 // for { // } + + es := "Go is a beautiful language." + fmt.Println("RawStr[", es, "] Length", len(es)) + for i := 0; i < len(es); i++ { + fmt.Printf(" >Character on position %02d is: '%c' \n", i, es[i]) + } + + us := "Go语言是一个很好的程序设计语言. Absolutely!" + fmt.Println("RawStr[", us, "] Length", len(us)) + for i, r := range us { + fmt.Printf(" >Character on position %02d is: '%c' \n", i, r) + } + + fmt.Println("---------------------") + for i, r := range us { + fmt.Printf(" >[%02d] %5d %U '%c'\t% X\n", i, r, r, r, []byte(string(r))) + } } diff --git a/learn/go/func.go b/learn/go/func.go index a77fca7..ac41afc 100644 --- a/learn/go/func.go +++ b/learn/go/func.go @@ -15,7 +15,6 @@ import ( "time" ) - func main() { defer fmt.Println("Program Exited...") @@ -34,14 +33,13 @@ func main() { neg := Adder() // NOTICE 十次是两个实例的累计结果,不是独立函数调用结果 - for i:=0; i<10; i++ { + 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++ { + for i := 0; i < 10; i++ { fmt.Printf("%02d - %5d\n", i, fibonacci()) } } @@ -60,14 +58,13 @@ func Adder() func(int) int { return innerfunc } - func Fibonacci() func() int { a := 0 b := 1 // a, b := 0, 1 innerfunc := func() int { t := b - b = a+b + b = a + b a = t //a, b = b, a+b return a diff --git a/learn/go/hello.go b/learn/go/hello.go index 5ae0f15..6589509 100644 --- a/learn/go/hello.go +++ b/learn/go/hello.go @@ -3,6 +3,15 @@ package main import "fmt" import "math/rand" import "time" +import "os" +import "unicode" +import "runtime" + +// init 函数是非常特殊的函数,它不能被人为调用,而是在 +// 每个包完成初始化后自动执行,并且执行优先级比main函数高 +func init() { + fmt.Println("INIT FUNC") +} func GetStr() (string, string) { // 常量可以是数字、字符串、布尔或字符 @@ -56,7 +65,7 @@ var xa, xb, xc = true, "xb", 0xDD func main() { // defer 的参数会立刻生成,但是只是在程序结束时调用 - defer fmt.Println("----------------") + defer fmt.Println("----------------", runtime.GOOS) rand.Seed(time.Now().UnixNano()) n := 0 for i := 0; i < rand.Intn(10)+1; i++ { @@ -109,4 +118,13 @@ func main() { *pj = 123 fmt.Println(n, *pi, *pj, pi, pj) + + var HOME = os.Getenv("HOME") + var USER = os.Getenv("USER") + var GOPATH = os.Getenv("GOPATH") + + fmt.Println("HOME", HOME, "USER", USER, "GOPATH", GOPATH) + + var charA, charB, charC rune = 'a', '3', ' ' + fmt.Println(unicode.IsLetter(charA), unicode.IsDigit(charB), unicode.IsSpace(charC)) } diff --git a/learn/go/interfaces.go b/learn/go/interfaces.go index 2158b45..e038657 100644 --- a/learn/go/interfaces.go +++ b/learn/go/interfaces.go @@ -7,11 +7,12 @@ * ------------------------------------------------------------------------ */ package main + import ( - "fmt" + "fmt" ) func main() { - defer fmt.Println("Program Exited...") + defer fmt.Println("Program Exited...") } diff --git a/learn/go/iota.go b/learn/go/iota.go new file mode 100644 index 0000000..6d050b6 --- /dev/null +++ b/learn/go/iota.go @@ -0,0 +1,39 @@ +/* + * ------------------------------------------------------------------------ + * File Name: iota.go + * Author: Zhao Yanbai + * 2015-11-28 14:23:24 Saturday CST + * Description: none + * ------------------------------------------------------------------------ + */ +package main + +import ( + "fmt" +) + +const ( + _ = iota + KB = 1 << (10 * iota) + MB + GB + TB + PB + EB + ZB + YB +) + +const ( + Active = 1 << iota + Send + Receive +) + +func main() { + defer fmt.Println("Program Exited...") + + fmt.Printf("0x%X 0x%X 0x%X 0x%X\n", KB, MB, GB, TB) + + fmt.Printf("0x%X 0x%X 0x%X\n", Active, Send, Receive) +} diff --git a/learn/go/methods.go b/learn/go/methods.go index d7487da..8ceec3f 100644 --- a/learn/go/methods.go +++ b/learn/go/methods.go @@ -7,18 +7,18 @@ * ------------------------------------------------------------------------ */ package main + import ( - "fmt" - "math" - "time" - "math/rand" + "fmt" + "math" + "math/rand" + "time" ) type Vector struct { x, y float64 } - // 不用指针型也可以 // 此处于C语言同理,用指针能改变则用指针拷贝 // 用值则用值拷贝,指针一般更有效率,可以修改 @@ -29,8 +29,8 @@ func (v *Vector) Abs() float64 { // 可以将v换成非指针型对比 func (v *Vector) Scale(scale float64) { - v.x = v.x*scale - v.y = v.y*scale + v.x = v.x * scale + v.y = v.y * scale } // fmt中的一个接口 @@ -42,16 +42,15 @@ func (v Vector) String() string { return fmt.Sprintf("Vector{x:%.4f, y:%.4f}", v.x, v.y) } - // 根据现有类型定义专用类型 -type FloatType float64; +type FloatType float64 func (f FloatType) TenTimesInt() int { - return int(f*10) + return int(f * 10) } func main() { - defer fmt.Println("Program Exited...") + defer fmt.Println("Program Exited...") rand.Seed(time.Now().UnixNano()) @@ -70,7 +69,6 @@ func main() { 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()) } diff --git a/learn/go/string.go b/learn/go/string.go new file mode 100644 index 0000000..b11352e --- /dev/null +++ b/learn/go/string.go @@ -0,0 +1,75 @@ +/* + * ------------------------------------------------------------------------ + * File Name: string.go + * Author: Zhao Yanbai + * 2015-11-28 14:44:55 Saturday CST + * Description: none + * ------------------------------------------------------------------------ + */ +package main + +import ( + "fmt" + "strconv" + "strings" +) + +func main() { + defer fmt.Println("Program Exited...") + + interpretString := "这是一个解释型字符串.\n\\n会换行" + fmt.Println(interpretString) + + nonInterpretString := `这是一个解释型字符串.\n不会换行` + fmt.Println(nonInterpretString) + + // 纯ASCII字符串可以通过标准索引法来获取 如s[i] + // 对字符串取地址非法 &s[i] + + var str string = "This is an example of a string. [这是一个中文示例字符串]" + fmt.Println(strings.HasPrefix(str, "This")) + fmt.Println(strings.HasSuffix(str, "string")) + fmt.Println(strings.Contains(str, "example")) + fmt.Println(strings.LastIndex(str, "example")) + fmt.Println(strings.IndexRune(str, '中')) + fmt.Println(strings.Index(str, "hello")) + fmt.Println(strings.Count(str, "a")) + + str = strings.Replace(str, "example", "示例", -1) // -1表示替换所有 + fmt.Println(str) + + var repeat string = "Repeat " + fmt.Println("Repeat:", strings.Repeat(repeat, 10)) + fmt.Println("ToUpper:", strings.ToUpper(repeat)) + fmt.Println("ToLower:", strings.ToLower(repeat)) + + str = " This is a string " + fmt.Printf("RawStr:[%s]\n", str) + fmt.Printf("TrimLeft:[%s]\n", strings.TrimLeft(str, " ")) + fmt.Printf("TrimRight:[%s]\n", strings.TrimRight(str, " ")) + fmt.Printf("TrimSpace:[%s]\n", strings.TrimSpace(str)) + + str = "------This is another string---" + fmt.Printf("RawStr:[%s]\n", str) + fmt.Printf("Trim:[%s]\n", strings.Trim(str, "--")) + + str = "This-is-a-string-to-be-splited" + fmt.Printf("RawStr:[%s]\n", str) + for _, s := range strings.Split(str, "-") { + fmt.Println(s) + } + + str = "This is a string to be splited into word by strins.Fields" + fmt.Printf("RawStr:[%s]\n", str) + strfields := strings.Fields(str) + for _, s := range strfields { + fmt.Println(s) + } + fmt.Printf("JoinStr By ':' : [%s]\n", strings.Join(strfields, ":")) + + fmt.Println("strconv Itoa: ", strconv.Itoa(123)) + val, err := strconv.Atoi("321") + if err == nil { + fmt.Println("strconv Atoi: ", val) + } +} diff --git a/learn/go/switch.go b/learn/go/switch.go index 7af3e24..fd8b6c3 100644 --- a/learn/go/switch.go +++ b/learn/go/switch.go @@ -7,6 +7,12 @@ import ( "time" ) +func getTwoRandInt() (a, b int) { + a = rand.Int() + b = rand.Int() + return +} + func main() { rand.Seed(time.Now().UnixNano()) fmt.Print("Go runs on ") @@ -50,4 +56,12 @@ func main() { fmt.Println("Ground") } + switch a, b := getTwoRandInt(); { + case a < b: + fmt.Println(a, "<", b) + case a == b: + fmt.Println(a, "=", b) + case a > b: + fmt.Println(a, ">", b) + } } diff --git a/learn/go/var.go b/learn/go/var.go new file mode 100644 index 0000000..e93c356 --- /dev/null +++ b/learn/go/var.go @@ -0,0 +1,22 @@ +/* + * ------------------------------------------------------------------------ + * File Name: var.go + * Author: Zhao Yanbai + * 2015-11-28 13:47:31 Saturday CST + * Description: none + * ------------------------------------------------------------------------ + */ +package main + +import ( + "fmt" + "os" +) + +func main() { + defer fmt.Println("Program Exited...") + + var HOME = os.Getenv("HOME") + + fmt.Println(HOME) +} diff --git a/tools/comm/cs b/tools/comm/cs index 3c96990..f9cb854 100755 --- a/tools/comm/cs +++ b/tools/comm/cs @@ -92,10 +92,10 @@ foreach my $file (@ARGV) { } elsif($index eq "go") { $as .= "package main\n"; $as .= "import (\n"; - $as .= " \"fmt\"\n"; + $as .= "\t\"fmt\"\n"; $as .= ")\n\n"; $as .= "func main() {\n"; - $as .= " defer fmt.Println(\"Program Exited...\")\n\n"; + $as .= "\tdefer fmt.Println(\"Program Exited...\")\n\n"; $as .= "}"; $as = "" unless($create_main); } elsif($index eq "py") {