本文摘自php中文网,作者angryTom,侵删。
使用Golang开发web后台,需要接收前端传来的参数并作出响应,那么Golang该如何接收前端的参数呢?一起来看下吧。
Golang如何接收前端的参数
1、首先,创建一个Golang web服务。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package main
import (
"log"
"fmt"
"net/http"
"html/template"
)
func handleIndex(writer http.ResponseWriter, request *http.Request) {
t, _ := template.ParseFiles( "index.html" )
t.Execute(writer, nil)
}
func main() {
http.HandleFunc( "/" , handleIndex)
fmt.Println( "Running at port 3000 ..." )
err := http.ListenAndServe( ":3000" , nil)
if err != nil {
log.Fatal( "ListenAndServe: " , err.Error())
}
}
|
index.html
1 2 3 4 5 6 7 8 9 10 | <!DOCTYPE html>
<html>
<head>
<meta charset= "UTF-8" >
<title>Document</title>
</head>
<body>
Golang GET&POST
</body>
</html>
|
2、然后编写前端get post请求,使用了axios库,请自行引入。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <script>
axios.get( '/testGet' , {
params: {
id: 1,
}
}).then((response) => {
console.log(response);
});
const postData = {
username: 'admin' ,
password: '123' ,
};
axios.post( '/testPostJson' , postData).then((response) => {
console.log(response);
});
</script>
|
3、接着,在Golang中实现接收get post参数即可。
阅读剩余部分
相关阅读 >>
go - httpclient 常用操作
golang语言社区--游戏服务器编程说明
golang 定时器详解
【gocn译文视点】何时使用 rust, 何时使用 go
关于golang封装一个bash函数,用于执行bash命令
golang 如何读取csv文件
go的切片(进阶版)
搭建coolpy+mqtt 服务
jenkins构建go及java项目
golang zip中文乱码怎么解决
更多相关阅读请进入《golang》频道 >>
老貘
一个与时俱进的Go编程知识库。
转载请注明出处:木庄网络博客 » Golang如何接收前端的参数