[Go] A simple Go server
Hello World
Let's create a simple Go server, first let's setup the server and do hello world
// @filename: main.go
package main
import (
"fmt"
"net/http"
)
func handleHello (res http.ResponseWriter, req *http.Request) {
// Covert to []bytpe type
res.Write([]byte("Hello from a Go program"))
}
func main {
server := http.NewServeMux()
server.HandleFunc("/hello", handleHello)
err := http.ListenAndServe(":3333", server)
if err != nil {
fmt.Println("Server cannot start")
}
}
After run go run .
, you can visit localhost:3333/hello
to see the server is running and return hello world.
Static HTML
Now let's serve a static HTML, let say we have a public
folder with an index.html
file inside.
We want to serve the public files on our serve
// @filename: main.go
package main
import (
"fmt"
"net/http"
)
func handleHello (res http.ResponseWriter, req *http.Request) {
// Covert to []bytpe type
res.Write([]byte("Hello from a Go program"))
}
func main {
server := http.NewServeMux()
server.HandleFunc("/hello", handleHello)
// Serve public files
fs := http.FileServer(http.Dir("./public"))
server.Handle("/", fs)
err := http.ListenAndServe(":3333", server)
if err != nil {
fmt.Println("Server cannot start")
}
}
Rerun the server, visit localhost:3333
you should be able to see the index.html file
Data Layer
// @filename: data/exhibitions.go
package data
type Exhibition struct {
Title string
Description string
Image string
CurrentlyOpened bool
}
var list = []Exhibition{
{
Title: "Life in Ancient Greek",
Description: "Uncover the world of ancient Greece through the sculptures, tools, and jewelry found in ruins from over 2000 years ago that have been unearthed through modern science and technology.",
Image: "ancient-greece.png",
CurrentlyOpened: false,
},
{
Title: "Aristotle: Life and Legacy",
Description: "Explore the life and legacy of the great philosopher Aristotle, one of the most influential thinkers of all time. Through rare artifacts and ancient texts, learn about his ideas on ethics, politics, and metaphysics that have shaped the world for centuries.",
Image: "aristotle.png",
CurrentlyOpened: true,
},
}
func GetAll() []Exhibition {
return list
}
func Add(exb Exhibition) {
list = append(list, exb)
}
We can test the data layer by add two api endpoints
GET: /api/exhibitions
POST: /api/exhibitions/new
API Layer
// @filename: api/get.go
package api
import (
"encoding/json"
"net/http"
"strconv"
"project/museum/data"
)
func Get(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
// /api/exhibitions?id=1
id := r.URL.Query()["id"]
if id != nil {
finalId, err := strconv.Atoi(id[0]) // parseInt
if err == nil && finalId < len(data.GetAll()) {
// Send one item back to Client using json encoder
json.NewEncoder(w).Encode(data.GetAll()[finalId])
} else {
http.Error(w, "Invalid Exhibition", http.StatusBadRequest)
}
} else {
// Send all item to client
json.NewEncoder(w).Encode(data.GetAll())
}
}
// @filename: api/post.go
package api
import (
"encoding/json"
"net/http"
"project/museum/data"
)
func Post(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost {
var item data.Exhibition
// Read data from client
err := json.NewDecoder(r.Body).Decode(&item)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
data.Add(item)
w.WriteHeader(http.StatusCreated)
w.Write(([]byte("OK")))
} else {
http.Error(w, "Unsupported Method", http.StatusMethodNotAllowed)
}
}
Now we can link those two api in our server
// @filename: main.go
package main
import (
"fmt"
"net/http"
"project/museum/api"
"project/museum/data"
)
func handleHello (res http.ResponseWriter, req *http.Request) {
// Covert to []bytpe type
res.Write([]byte("Hello from a Go program"))
}
func main {
server := http.NewServeMux()
server.HandleFunc("/hello", handleHello)
// Get and Post
server.handleFunc("/api/exhibitions", api.Get)
server.handleFunc("/api/exhibitions", api.Post)
// Serve public files
fs := http.FileServer(http.Dir("./public"))
server.Handle("/", fs)
err := http.ListenAndServe(":3333", server)
if err != nil {
fmt.Println("Server cannot start")
}
}
Rerun the server, then you can use POSTMAN to check apis.
Template
Now we want to display the data in our client by using template, so that we can do server side rendering which is more performance compare to Client side data fetching + rendering
// @filename: templates/index.tmpl
<!DOCTYPE html>
<html lang="en">
<head>
...
</head>
<body>
<main>
{{ range .}}
<article
class="{{- if .CurrentlyOpened -}} opened {{- else -}} closed {{- end -}}"
>
<h2>{{ .Title }}</h2>
<p>
{{ .Description }}
</p>
<img src="gallery/{{ .Image }}" fetchpriority="high" decoding="sync" />
</article>
{{ end }}
</main>
</body>
</html>
So .
means the whole data, because we will return an array of object, therefore we use range
to foreach object, so the .
represent each object
And you can see, it support logic condition {{ if cond }} A {{ else }} B {{ end }}
If you want to remove all the whie space, you can use -
, then it becomes {{- if .CurrentlyOpened -}} opened {{- else -}} closed {{- end -}}
Let's serve the template
// @filename: main.go
package main
import (
"fmt"
"net/http"
"text/template"
"project/museum/api"
"project/museum/data"
)
func handleHello (res http.ResponseWriter, req *http.Request) {
res.Write([]byte("Hello from a Go program"))
}
func handleTemplate(res http.ResponseWriter, req *http.Request) {
html, err := template.ParseFiles("templates/index.tmpl")
if err != nil {
res.WriteHeader(http.StatusInternalServerError)
res.Write([]byte("Internal Server Error"))
return
}
// Send data to template
html.Execute(res, data.GetAll())
}
func main() {
// Create a route handler
server := http.NewServeMux()
server.HandleFunc("/hello", handleHello)
// Get and Post
server.HandleFunc("/api/exhibitions", api.Get)
server.HandleFunc("/api/exhibitions/new", api.Post)
// template
server.HandleFunc("/templates", handleTemplate)
// Server public files
fs := http.FileServer(http.Dir("./public"))
server.Handle("/", fs)
err := http.ListenAndServe(":3333", server)
if err != nil {
fmt.Println("Server cannot start")
}
}
DONE. If you change template and css, js you don't need to restart the Go server
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2021-02-09 [Bash] Chain Commands with Pipes and Redirect Output in Bash
2021-02-09 [Bash] Use Conditional Statements in Bash
2018-02-09 [ReactVR] Animate Text, Images, Views, and 3D Elements Using the Animated Library in React VR
2016-02-09 [Cycle.js] Fine-grained control over the DOM Source
2016-02-09 [Cycle.js] Making our toy DOM Driver more flexible
2016-02-09 [Redux] Generating Containers with connect() from React Redux (VisibleTodoList)
2016-02-09 [MongoDB] Remove, update, create document