This article describes the method of Go language web programming to implement the sending and parsing of Get and Post requests. Share it for your reference, as follows:
This is an introductory article, which introduces the technologies mainly used in Golang's web programming through a simple example.
Article structure includes:
1. Client-Get Request
2. Client-Post Request
3. Server handles Get and Post data
In the data encapsulation, we partially use json, so this article also involves the encoding and decoding of json in Golang.
1. Client-Get
Copy the codeThe code is as follows:
package main
import (
"fmt"
"net/url"
"net/http"
"io/ioutil"
"log"
)
func main() {
u, _ := ("http://localhost:9001/xiaoyue")
q := ()
("username", "user")
("password", "passwd")
= ()
res, err := (());
if err != nil {
(err) return
}
result, err := ()
()
if err != nil {
(err) return
}
("%s", result)
}
import (
"fmt"
"net/url"
"net/http"
"io/ioutil"
"log"
)
func main() {
u, _ := ("http://localhost:9001/xiaoyue")
q := ()
("username", "user")
("password", "passwd")
= ()
res, err := (());
if err != nil {
(err) return
}
result, err := ()
()
if err != nil {
(err) return
}
("%s", result)
}
2. Client-Post
Copy the codeThe code is as follows:
package main
import (
"fmt"
"net/url"
"net/http"
"io/ioutil"
"log"
"bytes"
"encoding/json"
)
type Server struct {
ServerName string
ServerIP string
}
type Serverslice struct {
Servers []Server
ServersID string
}
func main() {
var s Serverslice
var newServer Server;
= "Guangzhou_VPN";
= "127.0.0.1"
= append(, newServer)
= append(, Server{ServerName: "Shanghai_VPN", ServerIP: "127.0.0.2"})
= append(, Server{ServerName: "Beijing_VPN", ServerIP: "127.0.0.3"})
= "team1"
b, err := (s)
if err != nil {
("json err:", err)
}
body := ([]byte(b))
res,err := ("http://localhost:9001/xiaoyue", "application/json;charset=utf-8", body)
if err != nil {
(err)
return
}
result, err := ()
()
if err != nil {
(err)
return
}
("%s", result)
}
import (
"fmt"
"net/url"
"net/http"
"io/ioutil"
"log"
"bytes"
"encoding/json"
)
type Server struct {
ServerName string
ServerIP string
}
type Serverslice struct {
Servers []Server
ServersID string
}
func main() {
var s Serverslice
var newServer Server;
= "Guangzhou_VPN";
= "127.0.0.1"
= append(, newServer)
= append(, Server{ServerName: "Shanghai_VPN", ServerIP: "127.0.0.2"})
= append(, Server{ServerName: "Beijing_VPN", ServerIP: "127.0.0.3"})
= "team1"
b, err := (s)
if err != nil {
("json err:", err)
}
body := ([]byte(b))
res,err := ("http://localhost:9001/xiaoyue", "application/json;charset=utf-8", body)
if err != nil {
(err)
return
}
result, err := ()
()
if err != nil {
(err)
return
}
("%s", result)
}
3. Server
Copy the codeThe code is as follows:
package main
import (
"fmt"
"net/http"
"strings"
"html"
"io/ioutil"
"encoding/json"
)
type Server struct {
ServerName string
ServerIP string
}
type Serverslice struct {
Servers []Server
ServersID string
}
func main() {
("/", handler)
(":9001", nil)
}
func handler(w , r *) {
() //Parse parameters, the default will not be parsed
(w, "Hi, I love you %s", ([1:]))
if == "GET" {
("method:", ) //Method to obtain the request
("username", ["username"])
("password", ["password"])
for k, v := range {
("key:", k, "; ")
("val:", (v, ""))
}
} else if == "POST" {
result, _:= ()
()
("%s\n", result)
//Recommended processing method for unknown types
var f interface{}
(result, &f)
m := f.(map[string]interface{})
for k, v := range m {
switch vv := v.(type) {
case string:
(k, "is string", vv)
case int:
(k, "is int", vv)
case float64:
(k,"is float64",vv)
case []interface{}:
(k, "is an array:")
for i, u := range vv {
(i, u)
}
default:
(k, "is of a type I don't know how to handle")
}
}
//The structure is known, parsed to the structure
var s Serverslice;
([]byte(result), &s)
();
for i:=0; i<len(); i++ {
([i].ServerName)
([i].ServerIP)
}
}
}
import (
"fmt"
"net/http"
"strings"
"html"
"io/ioutil"
"encoding/json"
)
type Server struct {
ServerName string
ServerIP string
}
type Serverslice struct {
Servers []Server
ServersID string
}
func main() {
("/", handler)
(":9001", nil)
}
func handler(w , r *) {
() //Parse parameters, the default will not be parsed
(w, "Hi, I love you %s", ([1:]))
if == "GET" {
("method:", ) //Method to obtain the request
("username", ["username"])
("password", ["password"])
for k, v := range {
("key:", k, "; ")
("val:", (v, ""))
}
} else if == "POST" {
result, _:= ()
()
("%s\n", result)
//Recommended processing method for unknown types
var f interface{}
(result, &f)
m := f.(map[string]interface{})
for k, v := range m {
switch vv := v.(type) {
case string:
(k, "is string", vv)
case int:
(k, "is int", vv)
case float64:
(k,"is float64",vv)
case []interface{}:
(k, "is an array:")
for i, u := range vv {
(i, u)
}
default:
(k, "is of a type I don't know how to handle")
}
}
//The structure is known, parsed to the structure
var s Serverslice;
([]byte(result), &s)
();
for i:=0; i<len(); i++ {
([i].ServerName)
([i].ServerIP)
}
}
}
I hope this article will be helpful to everyone's Go language programming.