Golang
Golang表单的部分,需要使用两个档案的方案来demo
首先在资料夹中随意建立一个.html结尾的档案
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>表单</title>
</head>
<body>
<form action="http://localhost:8000/" method="get">
<p>First name: <input type="text" name="first name" /></p>
<p>Last name: <input type="text" name="last name" /></p>
<input type="submit" value="Submit" />
</form>
</body>
</html>
要注意的是action 必须跟你下一个步骤的golang档案所开的url是相呼应的,所以如果跑起来没反应,注意一下port是不是相同
package main
import (
"fmt"
"log"
"net/http"
)
func GetForm(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
for k, v := range r.Form {
fmt.Println("key is: ", k)
fmt.Println("val is: ", v)
fmt.Fprintf(w, "key is: %s \n", k)
fmt.Fprintf(w, "val is: %s \n", v)
}
fmt.Fprintf(w, "hello world")
}
func main() {
http.HandleFunc("/", GetForm)
err := http.ListenAndServe("localhost:8000", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
这样我们就简单地跑了一个简单的接收HTML表单的程序罗!!
要特别注意的部分是
fmt.Println("key is: ", k)
fmt.Fprintf(w, "key is: %s \n", k)
一个是在终端机上输出,一个是在浏览器上输出唷!
如果我们要在golang使用json的话,需要引用encodeing/json这个套件
package main
import (
"fmt"
"encoding/json"
)
type response1 struct {
Id int
Os []string
}
func main() {
Page_json := response1 {Id : 2, Os: []string{"windows", "mac", "linux"}}
byteSlice, _ := json.MarshalIndent(Page_json , "", " ")
fmt.Println(string(byteSlice))
}
然後我在网路上还看到类似的写法(Marshal与MarshalIndent)
res, _ := json.Marshal(Page_json)
fmt.Println(string(res))
res, _ = json.MarshalIndent(Page_json , "", " ")
fmt.Println(string(res))
在Go Playground上跑看看结果一下,看是差异在哪边?
res, _ := json.Marshal(Page_json)
{"Id":2,"Os":["windows","mac","linux"]}
res, _ = json.MarshalIndent(Page_json , "", " ")
{
"Id": 2,
"Os": [
"windows",
"mac",
"linux"
]
}
>>: 【从实作学习ASP.NET Core】Day02 | MVC 的架构
比对List和SortedList 比对容器 List SortedList 比较方法 资料放入容器...
C语言中还有另外两种类似 struct 的指令 union 和 struct 一样可以由多个不同型别...
文章选自 作者:xiaohesong 连接:https://juejin.im/post/68449...
连结即使是很重要的事,但似乎大部份的人只关心外站的反向连结,而却忽略内部的正向连结,事实上内部连结才...
其实贪心只是一种思路,时常体现在各种算法里面,像之前讲的最小生成树(prim),最短路径(Dijks...