go語言實現(xiàn)簡單http服務的方法
更新時間:2015年02月27日 09:58:36 作者:pythoner
這篇文章主要介紹了go語言實現(xiàn)簡單http服務的方法,涉及Go語言http操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了go語言實現(xiàn)簡單http服務的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
復制代碼 代碼如下:
package main
import (
"flag"
"log"
"net/http"
"text/template"
)
var addr = flag.String("addr", ":1718", "http service address") // Q=17, R=18
var templ = template.Must(template.New("qr").Parse(templateStr))
func main() {
flag.Parse()
http.Handle("/", http.HandlerFunc(QR))
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Fatal("ListenAndServe:", err)
}
}
func QR(w http.ResponseWriter, req *http.Request) {
templ.Execute(w, req.FormValue("s"))
}
const templateStr = `
<html>
<head>
<title>QR Link Generator</title>
</head>
<body>
{{if .}}
<img src="http://chart.apis.google.com/chart?chs=300x300&cht=qr&choe=UTF-8&chl={{urlquery .}}" />
<br>
{{html .}}
<br>
<br>
{{end}}
<form action="/" name=f method="GET"><input maxLength=1024 size=70
name=s value="" title="Text to QR Encode"><input type=submit
value="Show QR" name=qr>
</form>
</body>
</html>
import (
"flag"
"log"
"net/http"
"text/template"
)
var addr = flag.String("addr", ":1718", "http service address") // Q=17, R=18
var templ = template.Must(template.New("qr").Parse(templateStr))
func main() {
flag.Parse()
http.Handle("/", http.HandlerFunc(QR))
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Fatal("ListenAndServe:", err)
}
}
func QR(w http.ResponseWriter, req *http.Request) {
templ.Execute(w, req.FormValue("s"))
}
const templateStr = `
<html>
<head>
<title>QR Link Generator</title>
</head>
<body>
{{if .}}
<img src="http://chart.apis.google.com/chart?chs=300x300&cht=qr&choe=UTF-8&chl={{urlquery .}}" />
<br>
{{html .}}
<br>
<br>
{{end}}
<form action="/" name=f method="GET"><input maxLength=1024 size=70
name=s value="" title="Text to QR Encode"><input type=submit
value="Show QR" name=qr>
</form>
</body>
</html>
希望本文所述對大家的Go語言程序設計有所幫助。
相關文章
Go?Gin框架優(yōu)雅重啟和停止實現(xiàn)方法示例
Web應用程序中,有時需要重啟或停止服務器,無論是因為更新代碼還是進行例行維護,這時需要保證應用程序的可用性和數(shù)據(jù)的一致性,就需要優(yōu)雅地關閉和重啟應用程序,即不丟失正在處理的請求和不拒絕新的請求,本文將詳解如何在Go語言中使用Gin這個框架實現(xiàn)優(yōu)雅的重啟停止2024-01-01利用ChatGPT編寫一個Golang圖像壓縮函數(shù)
這篇文章主要為大家詳細介紹了如何利用ChatGPT幫我們寫了一個Golang圖像壓縮函數(shù),文中的示例代碼簡潔易懂,感興趣的小伙伴可以嘗試一下2023-04-04Golang判斷struct/slice/map是否相等以及對比的方法總結
平時開發(fā)中對比兩個struct或者map、slice是否相等是經常遇到的,有很多對比的方式,比如==,reflect.DeepEqual(),cmp.Equal()等也是經常容易混淆的,這么多種對比方式,適用場景和優(yōu)缺點都有哪些呢?今天我們來具體總結一下,感興趣的小伙伴們可以參考借鑒2022-11-11