分享
Go 学习笔记(一)
ibyte · · 1849 次点击 · · 开始浏览这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。
随着Go的应用越来越火热,自己也终于开始学习了。平时经常用C,看着Go还是比较亲切的。好了,开始。
今天主要是按照树上的内容自己简单的实践了下最基本的输出,以及网页功能,上代码:
1 package main 2 3 import ( 4 "fmt" 5 ) 6 7 func main() { 8 fmt.Printf("Hello world\n") 9 }
加法运算代码:
1 package main 2 3 import ( 4 "fmt" 5 ) 6 7 func add(a int,b int)(c int){ 8 c= a+b 9 return c 10 } 11 12 13 func main() { 14 c:=add(1,2) 15 fmt.Println(c) 16 }
网页"Hello world"代码:
1 package main 2 3 import ( 4 "fmt" 5 "net/http" 6 ) 7 func sayHelloName(w http.ResponseWriter,r *http.Request){ 8 fmt.Fprintf(w,"hello,world") 9 10 } 11 12 func main() { 13 14 http.HandleFunc("/",sayHelloName) 15 }
登录截图:
网页登录代码:
1 package main 2 3 import ( 4 "fmt" 5 "html/template" 6 "net/http" 7 "log" 8 ) 9 10 func login(w http.ResponseWriter,r* http.Request){ 11 12 fmt.Println("method:", r.Method) 13 if r.Method == "GET"{ 14 t,_:=template.ParseFiles("/Users/mac/IdeaProjects/go1/login.gtpl") 15 t.Execute(w,nil) 16 }else{ 17 r.ParseForm() 18 fmt.Println("username:",r.Form["username"]) 19 fmt.Println("password",r.Form["password"]) 20 } 21 } 22 23 func main() { 24 http.HandleFunc("/login",login) 25 err:=http.ListenAndServe(":9090",nil) 26 if err != nil { 27 log.Fatal("ListenAndServe: ", err) 28 } 29 }
运行结果截图:
consle截图:
这里需要注意的是,程序在Mac环境下,网页模板路径需要使用绝对路径"/Users/mac/IdeaProjects/go1/login.gtpl" ,不然会报如下错误:
runtime error: invalid memory address or nil pointer dereference goroutine 5
有疑问加站长微信联系(非本文作者)
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
关注微信1849 次点击
上一篇:Go学习笔记-协程
添加一条新回复
(您需要 后才能回复 没有账号 ?)
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码` - 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传
收入到我管理的专栏 新建专栏
随着Go的应用越来越火热,自己也终于开始学习了。平时经常用C,看着Go还是比较亲切的。好了,开始。
今天主要是按照树上的内容自己简单的实践了下最基本的输出,以及网页功能,上代码:
1 package main 2 3 import ( 4 "fmt" 5 ) 6 7 func main() { 8 fmt.Printf("Hello world\n") 9 }
加法运算代码:
1 package main 2 3 import ( 4 "fmt" 5 ) 6 7 func add(a int,b int)(c int){ 8 c= a+b 9 return c 10 } 11 12 13 func main() { 14 c:=add(1,2) 15 fmt.Println(c) 16 }
网页"Hello world"代码:
1 package main 2 3 import ( 4 "fmt" 5 "net/http" 6 ) 7 func sayHelloName(w http.ResponseWriter,r *http.Request){ 8 fmt.Fprintf(w,"hello,world") 9 10 } 11 12 func main() { 13 14 http.HandleFunc("/",sayHelloName) 15 }
登录截图:
网页登录代码:
1 package main 2 3 import ( 4 "fmt" 5 "html/template" 6 "net/http" 7 "log" 8 ) 9 10 func login(w http.ResponseWriter,r* http.Request){ 11 12 fmt.Println("method:", r.Method) 13 if r.Method == "GET"{ 14 t,_:=template.ParseFiles("/Users/mac/IdeaProjects/go1/login.gtpl") 15 t.Execute(w,nil) 16 }else{ 17 r.ParseForm() 18 fmt.Println("username:",r.Form["username"]) 19 fmt.Println("password",r.Form["password"]) 20 } 21 } 22 23 func main() { 24 http.HandleFunc("/login",login) 25 err:=http.ListenAndServe(":9090",nil) 26 if err != nil { 27 log.Fatal("ListenAndServe: ", err) 28 } 29 }
运行结果截图:
consle截图:
这里需要注意的是,程序在Mac环境下,网页模板路径需要使用绝对路径"/Users/mac/IdeaProjects/go1/login.gtpl" ,不然会报如下错误:
runtime error: invalid memory address or nil pointer dereference goroutine 5