1. 基本语法
1 2 3 4 5 6 7
| package main
import "fmt"
func main() { fmt.Println("Hello world!") }
|
- package main 是包名,每个应用程序都要有一个main的包
- import “fmt” 引入包
- func main() { 用的入口,
{
不能单独一行 - 如果变量名/方法名/Type/Struct等以大写开头,则等同于public,可以其他包在导入后进行调用
1.1 定义变量和常量
1 2 3 4 5 6 7 8 9 10
| var a int b := 2 var a, b, c int = 1, 2, 3 var a, b, c = 1, 2, false a, b, c := 1, 2, false var ( a int b int c bool )
|
第二种类似于C#中的var,根据值进行推断。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| const Hello string = "hello world"
const ( A = "d" B = 2 C = "p" D = A + C E = B * B, F )
func main() { fmt.Println(A, B, C, D, E, F) }
|
这是常量,定义方式与变量类似
2. 执行与打包
go run ***.go
直接运行go build ***.go
编译为二进制文件,windows环境会编译成exe
3. 其他
3.1 引入多个包
1 2 3 4 5 6 7 8 9 10
| package main
import ( "fmt" "net/http" )
func main() { fmt.Println("Hello world!") }
|
4. http服务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package main
import ( "fmt" "net/http" )
func main() { http.HandleFunc("/go", myHandler) http.ListenAndServe(":8088", nil) }
func myHandler(w http.ResponseWriter, r *http.Request) { fmt.Println(r.RemoteAddr, "连接成功") fmt.Println("method:", r.Method) fmt.Println("url:", r.URL.Path) fmt.Println("header:", r.Header) fmt.Println("body:", r.Body) w.Write([]byte("helloworld")) }
|
http.HandleFunc("/go", myHandler)
一个路由(controller的action),myHandler方法http.ListenAndServe(":8088", nil)
监听端口,8088w.Write([]byte("helloworld"))
返回的内容
运行后,访问http://127.0.0.1:8088/go
会返回helloworld
支持匿名方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package main
import ( "fmt" "net/http" )
func main() { http.HandleFunc("/go", func(w http.ResponseWriter, r *http.Request) { fmt.Println(r.RemoteAddr, "连接成功") fmt.Println("method:", r.Method) fmt.Println("url:", r.URL.Path) fmt.Println("header:", r.Header) fmt.Println("body:", r.Body) w.Write([]byte("helloworld")) }) http.ListenAndServe(":8088", nil) }
|
5. 调用
项目文件夹为d:\github\gotest
会在根目录下生成一个go.mod文件,在创建一个httpserv文件夹然后文件树目录如下:
1 2 3 4 5 6
| d:\github\gotest │-go.mod │-mainapp.go └─httpserv serv.go s1.go
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| package main
import ( "net/http"
serv "gotest/httpserv" )
func main() { http.HandleFunc("/go", serv.HelloHandler) http.ListenAndServe(":8088", nil) }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package serv
import ( "fmt" "net/http" )
func HelloHandler(w http.ResponseWriter, r *http.Request) { fmt.Println(r.RemoteAddr, "连接成功") Demo() fmt.Println("method:", r.Method) fmt.Println("url:", r.URL.Path) fmt.Println("header:", r.Header) fmt.Println("body:", r.Body) w.Write([]byte("helloworld")) }
|
1 2 3 4 5 6 7 8 9 10
| package serv
import ( "fmt" )
func Demo() { fmt.Println("demo") }
|