2013年2月25日
摘要:
1.处理表单的输入新增一个login.html文件<html><head><title></title></head><body><form action="http://127.0.0.1:9090/login?username=astaxie" method="post"> 用户名:<input type="text" name="username"> 密码:<input type="password
阅读全文
posted @ 2013-02-25 11:51
liubiaoren
阅读(372)
推荐(0)
摘要:
1 package main 2 3 import ( 4 "fmt" 5 "log" 6 "net/http" 7 "strings" 8 ) 9 10 //在浏览器输入http://localhost:909011 //可以看到浏览器页面输出了Hello astaxie!12 //可以换一个地址试试:http://localhost:9090/?url_long1=111&url_long2=22213 14 func sayhelloName(w http.ResponseWriter, r *htt
阅读全文
posted @ 2013-02-25 10:54
liubiaoren
阅读(217)
推荐(0)
2013年2月3日
摘要:
从文件读取(无缓冲) 1 package main 2 3 import ( 4 "fmt" 5 "os" 6 ) 7 8 func main() { 9 buf := make([]byte, 1024)10 //打开文件11 f, _ := os.Open("D:/123.txt")12 //确保关闭了f13 defer f.Close()14 for {15 n, _ := f.Read(buf)16 fmt.Println(n)17 if n == 0 {18 ...
阅读全文
posted @ 2013-02-03 09:58
liubiaoren
阅读(153)
推荐(0)
2013年2月2日
摘要:
goroutine 是一个普通的函数,go作为开头 1 package main 2 3 import ( 4 "fmt" 5 "time" 6 ) 7 8 func ready(w string, sec int) { 9 time.Sleep(time.Duration(sec) * time.Second)10 fmt.Println(w, "is ready!")11 }12 13 func main() {14 go ready("Tea", 2)15 go ready("Coffee"
阅读全文
posted @ 2013-02-02 22:37
liubiaoren
阅读(132)
推荐(0)
摘要:
1 package main 2 3 import ( 4 "fmt" 5 "reflect" 6 ) 7 8 type Person struct { 9 name string "namestr" //namestr是标签10 age int11 }12 13 func ShowTag(i interface{}) {14 //Elem()得到指针指向的值,Field(0).Tag得到了manestr15 switch t := reflect.TypeOf(i); t.Kind() {16 case reflect.Ptr:..
阅读全文
posted @ 2013-02-02 22:13
liubiaoren
阅读(274)
推荐(0)
2013年2月1日
摘要:
定义接口的类型,仅仅是方法的集合 1 package main 2 3 import ( 4 "fmt" 5 ) 6 7 type S struct{ i int } 8 9 func (p *S) Get() int { return p.i }10 11 func (p *S) Put(v int) { p.i = v }12 13 type R struct{ i int }14 15 func (p *R) Get() int { return p.i }16 17 func (p *R) Put(v int) { p.i = v }18 19 type I int
阅读全文
posted @ 2013-02-01 14:47
liubiaoren
阅读(137)
推荐(0)
摘要:
定义自己的类型type foo intstruct 1 package main 2 3 import "fmt" 4 5 type Skills []string 6 7 type Human struct { 8 name string 9 age int10 weight int11 }12 13 type Student struct {14 Human // 匿名字段,struct15 Skills // 匿名字段,自定义的类型string slice16 int // 内置类型作为匿名字...
阅读全文
posted @ 2013-02-01 11:24
liubiaoren
阅读(128)
推荐(0)
2013年1月31日
摘要:
GO有指针,但没有指针运算,通过类型作为前缀来定义一个指针‘*’:var p *int,现在p就是一个指向整数值的指针。所有新定义的变量被赋值为零值,而指针为nil,相当于其他语言的null让指针指向某些内容,可以使用取址操作符(&) 1 package main 2 3 import ( 4 "fmt" 5 ) 6 7 func main() { 8 var p *int //p是整型指针变量 9 fmt.Println(p)10 var i int11 p = &i //使得p指向i,把地址赋给p(i=*p)12 fmt.Println(...
阅读全文
posted @ 2013-01-31 23:48
liubiaoren
阅读(132)
推荐(0)
摘要:
1 // sqrt.go 2 package even 3 4 func Even(i int) bool { // 可导出函数 5 return i%2 == 0 6 } 7 8 func odd(i int) bool { //私有函数 9 return i%2 == 110 } 1 package main 2 3 import ( 4 "even" 5 "fmt" 6 ) 7 8 func main() { 9 fmt.Println(even.Even(23)) //不能调用odd,因为第一字母小写是私有10 }包的测试bin 都是生...
阅读全文
posted @ 2013-01-31 23:21
liubiaoren
阅读(134)
推荐(0)
摘要:
定义:func(p mytype) funcname(q int)(r,s int){ return 0,0 }p mytype:函数可以定义用于特定的类型,这种函数更加通俗的称呼是method。递归函数 1 package main 2 3 import ( 4 "fmt" 5 ) 6 7 func rec(i int) { 8 if i == 10 { 9 return10 }11 rec(i + 1)12 fmt.Printf("%d", i)13 }14 15 func main() {16 rec(0)17 }987...
阅读全文
posted @ 2013-01-31 13:59
liubiaoren
阅读(212)
推荐(0)