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'
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
// 死循环
// 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)))
+ }
}
"time"
)
-
func main() {
defer fmt.Println("Program Exited...")
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())
}
}
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
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) {
// 常量可以是数字、字符串、布尔或字符
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++ {
*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))
}
* ------------------------------------------------------------------------
*/
package main
+
import (
- "fmt"
+ "fmt"
)
func main() {
- defer fmt.Println("Program Exited...")
+ defer fmt.Println("Program Exited...")
}
--- /dev/null
+/*
+ * ------------------------------------------------------------------------
+ * 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)
+}
* ------------------------------------------------------------------------
*/
package main
+
import (
- "fmt"
- "math"
- "time"
- "math/rand"
+ "fmt"
+ "math"
+ "math/rand"
+ "time"
)
type Vector struct {
x, y float64
}
-
// 不用指针型也可以
// 此处于C语言同理,用指针能改变则用指针拷贝
// 用值则用值拷贝,指针一般更有效率,可以修改
// 可以将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中的一个接口
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())
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())
}
--- /dev/null
+/*
+ * ------------------------------------------------------------------------
+ * 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)
+ }
+}
"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 ")
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)
+ }
}
--- /dev/null
+/*
+ * ------------------------------------------------------------------------
+ * 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)
+}
} 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") {