请到我的个人博客看golang rest相关文章 http://xiaorui.cc
关于Go语言在服务端做Restful接口和socket通信
已经转到:
http://xiaorui.cc/2014/10/25/%E5%85%B3%E4%BA%8Ego%E8%AF%AD%E8%A8%80%E5%9C%A8%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%81%9Arestful%E6%8E%A5%E5%8F%A3%E5%92%8Csocket%E9%80%9A%E4%BF%A1/
下图即是REST的架构图:
当REST架构的约束条件作为一个整体应用时,将生成一个可以扩展到大量客户端的应用程序。它还降低了客户端和服务器之间的交互延迟。统一界面简化了整个系统架构,改进了子系统之间交互的可见性。REST简化了客户端和服务器的实现,而且对于使用REST开发的应用程序更加容易扩展。
其实说白了,就是根据不同的方法,来分类不同的任务~
package main
import (
"fmt"
"github.com/drone/routes"
"net/http"
)
func getuser(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
uid := params.Get(":uid")
fmt.Fprintf(w, "you are get user %s", uid)
}
func modifyuser(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
uid := params.Get(":uid")
fmt.Fprintf(w, "you are modify user %s", uid)
}
func deleteuser(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
uid := params.Get(":uid")
fmt.Fprintf(w, "you are delete user %s", uid)
}
func adduser(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
uid := params.Get(":uid")
fmt.Fprint(w, "you are add user %s", uid)
fmt.Printf("you are add user %s", uid)
}
//xiaorui.cc
func main() {
mux := routes.New()
mux.Get("/user/:uid", getuser)
mux.Post("/user/:uid", modifyuser)
mux.Del("/user/:uid", deleteuser)
mux.Put("/user/:uid", adduser)
http.Handle("/", mux)
http.ListenAndServe(":8088", nil)
}
//xiaorui.cc
结果:
接着咱们再说下 go的socket 。
客户端的情况
服务端的情况:
server.go
package main
import (
"fmt"
"net"
"os"
)
func main() {
var (
host = "127.0.0.1"
port = "8080"
remote = host + ":" + port
data = make([]byte, 1024)
)
fmt.Println("Initiating server... (Ctrl-C to stop)")
lis, err := net.Listen("tcp", remote)
defer lis.Close()
if err != nil {
fmt.Println("Error when listen: ", remote)
os.Exit(-1)
}
for {
var res string
conn, err := lis.Accept()
if err != nil {
fmt.Println("Error accepting client: ", err.Error())
os.Exit(0)
}
go func(con net.Conn) {
fmt.Println("New connection: ", con.RemoteAddr())
for {
length, err := con.Read(data)
if err != nil {
fmt.Printf("Client %v quit.\n", con.RemoteAddr())
con.Close()
return
}
res = string(data[0:length])
fmt.Printf("%s said: %s\n", con.RemoteAddr(), res)
res = "You said:" + res
con.Write([]byte(res))
}
}(conn)
}
}
client.go
package main
import (
"fmt"
"net"
"os"
)
var str string
var msg = make([]byte, 1024)
func main() {
var (
host = "127.0.0.1"
port = "8080"
remote = host + ":" + port
)
con, err := net.Dial("tcp", remote)
defer con.Close()
if err != nil {
fmt.Println("Server not found.")
os.Exit(-1)
}
fmt.Println("Connection OK.")
for {
fmt.Printf("Enter a sentence:")
fmt.Scanf("%s\n", &str)
if str == "quit" {
fmt.Println("Communication terminated.")
os.Exit(1)
}
in, err := con.Write([]byte(str))
if err != nil {
fmt.Printf("Error when send to server: %d\n", in)
os.Exit(0)
}
length, err := con.Read(msg)
if err != nil {
fmt.Printf("Error when read from server.\n")
os.Exit(0)
}
str = string(msg[0:length])
fmt.Println(str)
}
}
Go的并发模式
package main
import (
"fmt"
"runtime"
)
func say(s string) {
for i := 0; i < 5; i++ {
runtime.Gosched()
fmt.Println(s)
}
}
func main() {
go say("world")
say("end")
}
结果: