在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ GO/ Go 語言簡介
面向?qū)ο?/span>
Go 語言簡介
網(wǎng)頁下載
聊天室的開發(fā)
image 網(wǎng)站開發(fā)
基本語法
高級應(yīng)用

Go 語言簡介

從前接觸腳本語言不多,但是自從遇到 Go 之后,就開始慢慢喜歡上了這個腳本語言。Go 語言是 Google 設(shè)計,主要用來從事 Web 服務(wù)器側(cè)程序的開發(fā),學(xué)習(xí)起點低。一般熟練掌握 C、Python 的朋友花上幾個小時就可以學(xué)會Go 語言。

安裝環(huán)境

鑒于個人主要使用 linux 進(jìn)行工作,所以這里介紹的都是 linux 下的安裝方式。

centos: sudo yum install golang
ubuntu: sudo apt-get install golang

學(xué)習(xí)資源

本來學(xué)習(xí) Go 語言,最好的學(xué)習(xí)環(huán)境應(yīng)該是官方網(wǎng)站。所以,建議大家可以訪問一下 coolshell.cn網(wǎng)站,上面有 Go 語言的內(nèi)容,分別是個 Go 語言(上)、Go 語言(下)。

編譯方法

如果需要生成執(zhí)行文件,輸入 go build name.go, 其中 name.go 表示你需要編譯的那個文件名,這時會有一個執(zhí)行文件生成。

如果你需要立即看到效果,輸入 go run name.go 即可。

范例

e.1 add.go

package main  

import "fmt"  

func add(a int, b int)(c int) {  

        c =  a + b  
        return c  
}  

func main() {  

        c := add(1 ,2)  
        fmt.Println(c)  

}  

直接輸入 go run add.go 就可以打印效果了。

e.2 簡單 Web 服務(wù)器

package main  

import (  
        "fmt"  
        "net/http"  
)  

func sayHelloName(w http.ResponseWriter, r *http.Request) {  

        fmt.Fprintf(w, "hello, world")  
}  

func main() {  

        http.HandleFunc("/", sayHelloName)  
        http.ListenAndServe(":9090", nil)  

} 

這時一個簡單的 Web 服務(wù)器,首先 go run hello.go 之后,打開 os 下的一個 browser,輸入http://127.0.0.1:9090,你就會在網(wǎng)頁上看到 Web 的打印了。

e.3 帶有表單處理的 Web 服務(wù)器

package main  

import (  

        "fmt"  
        "html/template"  
        "net/http"  
)  

func sayHelloName(w http.ResponseWriter, r* http.Request) {  

        fmt.Fprintf(w, "hello, world")  
}  

func login(w http.ResponseWriter, r* http.Request) {  

        if r.Method == "GET" {  

                t, _ := template.ParseFiles("login.gtpl");  
                t.Execute(w, nil)  
        } else {  

                r.ParseForm()  
                fmt.Println("username:", r.Form["username"])  
                fmt.Println("password", r.Form["password"])  

        }  

}  

func main() {  

        http.HandleFunc("/", sayHelloName)  
        http.HandleFunc("/login", login)  
        http.ListenAndServe(":9090", nil)  
} 

上面給出的只是代碼內(nèi)容,你還需要一個 login.gtpl 模板文件,

[html] 
<html>  
<head>  
<title> </title>  
</head>  

<body>  
<form action="http://127.0.0.1:9090/login" method="post">  
        user: <input type="text" name ="username">  
        pass: <input type="password" name="password">  
        <input type="submit" value="login">  
</form>  
</body>  
</html>  

運行 go 代碼之后,試著在瀏覽器下輸入 127.0.0.1:9090 和 127.0.0.1:9090/login,你會有不同的驚喜。