Golang实现Web输入输出的简单示例
Golang实现Web输入输出的简单示例 谁能给我展示一个简单的可运行示例,说明如何使用 http/net 在网页上获取简单的用户输入并将其显示出来?
例如,仅在网页上实现以下功能:
fmt.Print("Enter text: ")
var input string
fmt.Scanln(&input)
fmt.Print(input)
8 回复
你想通过Skype、WhatsApp或其他方式来解答你的问题吗?这样会更方便。
更多关于Golang实现Web输入输出的简单示例的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
我是东方人,所以现在差不多是下午5点
非常感谢! 我很少看到能实际运行的示例。 我将其复制到谷歌云控制台,部署后它成功运行了!
这样安排很好;我平时周一到周五工作,早上8点到下午5点(有时会提前下班)。
你什么时间比较方便?
非常感谢您发送了一个可运行的示例!我知道这个要求可能有点过分,但您能否用带有注释说明各部分功能的相同代码来回复呢?
我创建了一个简单的登录示例。如果登录成功,它会显示相关值。
Main.go:
package main
import (
"html/template"
"log"
"net/http"
)
//Login Struct
type Login struct {
Name string
Pass string
}
//Login Example - Hello Guys - By Vitor Brunoo
var tpl *template.Template
func login(w http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodPost {
login := req.FormValue("login")
pass := req.FormValue("pass")
if login == "admin" && pass == "admin" {
var l Login
l.Name = req.FormValue("login")
l.Pass = req.FormValue("pass")
err := tpl.ExecuteTemplate(w, "login.gohtml", l)
if err != nil {
http.Error(w, err.Error(), 500)
log.Fatalln(err)
}
return
}
}
err := tpl.ExecuteTemplate(w, "login.gohtml", nil)
if err != nil {
http.Error(w, err.Error(), 500)
log.Fatalln(err)
}
}
func init() {
tpl = template.Must(template.ParseFiles("login.gohtml"))
}
func main() {
http.HandleFunc("/", login)
log.Fatal(http.ListenAndServe(":8080", nil))
}
Login.gohtml
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Login Exemple - Using template</title>
</head>
<body>
{{if .}}
<p> Hello {{.Name}} </p>
<script>
alert("Login done successfully");
</script>
{{end}}
{{if not .}}
<form method="POST">
<input type='text' name='login'>
<input type='password' name='pass'>
<input type='submit' value='GO' >
</form>
{{end}}
</body>
</html>
VitaoFoX/LoginGoLang
LoginGoLang - 使用GoLang实现的登录功能 - Web
以下是使用Go标准库net/http实现的一个简单Web应用示例,它通过HTML表单获取用户输入并在页面上显示。该示例包含一个处理函数,用于渲染表单和处理提交的数据。
package main
import (
"fmt"
"html/template"
"net/http"
)
// 定义用于渲染的HTML模板
const formTemplate = `
<!DOCTYPE html>
<html>
<head>
<title>Simple Input Example</title>
</head>
<body>
<h2>Enter Text:</h2>
<form method="POST" action="/">
<input type="text" name="inputText" placeholder="Type something...">
<input type="submit" value="Submit">
</form>
{{if .Display}}
<h3>You entered: {{.Text}}</h3>
{{end}}
</body>
</html>
`
// 用于存储模板数据的结构体
type TemplateData struct {
Text string
Display bool
}
func main() {
// 注册处理函数
http.HandleFunc("/", handleForm)
// 启动服务器在8080端口
fmt.Println("Server starting on :8080...")
http.ListenAndServe(":8080", nil)
}
func handleForm(w http.ResponseWriter, r *http.Request) {
// 解析模板
tmpl, err := template.New("form").Parse(formTemplate)
if err != nil {
http.Error(w, "Error parsing template", http.StatusInternalServerError)
return
}
data := TemplateData{}
// 检查是否为POST请求(表单提交)
if r.Method == http.MethodPost {
// 解析表单数据
err := r.ParseForm()
if err != nil {
http.Error(w, "Error parsing form", http.StatusBadRequest)
return
}
// 获取输入文本
inputText := r.FormValue("inputText")
data.Text = inputText
data.Display = true
}
// 执行模板,将数据渲染到响应
err = tmpl.Execute(w, data)
if err != nil {
http.Error(w, "Error rendering template", http.StatusInternalServerError)
return
}
}
运行步骤:
- 将上述代码保存为
main.go - 在终端运行:
go run main.go - 打开浏览器访问
http://localhost:8080
功能说明:
- 首次访问显示一个文本输入框和提交按钮
- 用户输入文本并提交后,页面会显示"您输入的内容:[用户输入的文本]"
- 每次提交后表单会保留,可以继续输入新内容
这个示例模拟了您提供的控制台代码的Web版本功能:
fmt.Print("Enter text: ")→ HTML表单和提示文本fmt.Scanln(&input)→ 表单输入字段和POST请求处理fmt.Print(input)→ 在页面上显示用户输入的内容
模板中的{{if .Display}}条件确保只有在用户提交表单后才显示输入的内容。


