package main
import (
"fmt"
"net/http"
)
//路由
func handler(w http.ResponseWriter, r *http.Request){
fmt.Fprintf(w, "hello World!")
}
func main(){
//路由
http.HandleFunc("/", handler)
//开启服务
http.ListenAndServe(":8091", nil) //port:8091(自定义,注意:要是空闲端口)
}
#启动服务
go run main.go
这样本地的8091端口就可以访问了
我们这里拿video来举例
package main
import (
"fmt"
"net/http"
)
func main(){
//静态文件访问
fs := http.FileServer(http.Dir("./assets"))
http.Handle("/static/", http.StripPrefix("/static/", fs))
//开启服务
http.ListenAndServe(":8091", nil)
}
按照图上的访问路径(http://localhost:8091/static/video/20231204-134423.mp4)
package main
import (
"fmt"
"time"
)
func main(){
currentTime := time.Now()
fmt.Println("当前的时间为",currentTime)
}
因为currentTime是
time.Time
类型所以要想页面输出要进行字符串转换
//获取时间
func getNowTime(w http.ResponseWriter, r *http.Request){
currentTime := time.Now()
timeString := currentTime.Format("2006-01-02 15:04:05")//"2006-01-02 15:04:05"只是时间格式化字符串格式
fmt.Fprintf(w,timeString)
}
至于这个函数上面命名也是如此,就当是
net/http
路由服务的固定格式便好
注意: Golang函数首字符大写代表着公开(public)小写代表着私有(private)
package main
import (
"fmt"
"time"
// "encoding/json"
"os"
)
func CreateFileNil(){
currentTime := GetTime()
fileName := currentTime.Format("2006-01-02_15-04-05") + ".json"
file,err := os.Create("./assets/json/"+fileName)
if err != nil {
fmt.Println("JSON编码失败:", err)
return
}
defer file.Close() //defer:表示函数的最后执行(这里是确保最后文件是关闭的)
}
func main(){
CreateFileNil()
}
package main
import (
"fmt"
"time"
"encoding/json"
"os"
)
type Person struct {
Name string
Age int
}
//创建一个固定值的json文件
func CreateFileGuDin(){
currentTime := GetTime()
fileName := currentTime.Format("2006-01-02_15-04-05") + ".json"
file,err := os.Create("./assets/json/"+fileName)
if err != nil {
fmt.Println("JSON编码失败:", err)
return
}
defer file.Close()
person := Person{Name: "Alice", Age: 25}
jsonData, err := json.Marshal(person)
if err != nil {
fmt.Println("JSON编码失败:", err)
return
}
_, err = file.Write(jsonData)
if err != nil {
fmt.Println("写入文件失败:", err)
return
}
fmt.Println("JSON文件创建成功:", fileName)
}
func main(){
CreateFileGuDin()
}
package main
import (
"fmt"
"net/http"
)
//路由
func handler(w http.ResponseWriter, r *http.Request){
fmt.Fprintf(w, "hello World!")
}
func main(){
//创建一个处理跨域请求的处理器函数
corsHandler := func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// 设置允许跨域的域名
w.Header().Set("Access-Control-Allow-Origin", "*")
// 允许的请求方法
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
// 允许的请求头
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
// 如果是预检请求,直接返回
if r.Method == "OPTIONS" {
return
}
h.ServeHTTP(w, r)
})
}
//路由
http.Handle("/", corsHandler(http.HandlerFunc(handler)))
//开启服务
http.ListenAndServe(":8091", nil)
}
package main
import (
"fmt"
"net/http"
"encoding/json"
)
type FileDetail struct{
NowTime string `json:"now_time"`
FileName string `json:"file_name"`
}
func func_name1(w http.ResponseWriter, r *http.Request){
responseData := FileDetail {NowTime:"2023_12_04_22_51_30.json",FileName:"http://"+local+":" + port + "/static/video/20231204-19.mp4"}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(responseData)
}
func main(){
//创建一个处理跨域请求的处理器函数
corsHandler := func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// 设置允许跨域的域名
w.Header().Set("Access-Control-Allow-Origin", "*")
// 允许的请求方法
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
// 允许的请求头
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
// 如果是预检请求,直接返回
if r.Method == "OPTIONS" {
return
}
h.ServeHTTP(w, r)
})
}
http.Handle("/test", corsHandler(http.HandlerFunc(func_name1)))
//开启服务
http.ListenAndServe(":8091", nil)
}