(六)ServeMux示例

package main

import (
	"fmt"
	"net/http"
)

type DB struct{
	name string
	age int
}

var gdb DB = DB{"catty",26}

func get(w http.ResponseWriter, req *http.Request) {
	// The "/" pattern matches everything, so we need to check
	// that we're at the root here.
	if req.URL.Path != "/get" {
		http.NotFound(w, req)
		return
	}
	fmt.Fprintf(w, "Welcome to the home page!\n")
	fmt.Fprintf(w, "name is %s, age is %d\n",gdb.name,gdb.age)
}

func (db *DB)list(w http.ResponseWriter, req *http.Request) {
	if req.URL.Path != "/list" {
		http.NotFound(w, req)
		return
	}
	fmt.Fprintf(w, "Welcome to the home page!\n")
	fmt.Fprintf(w, "name is %s, age is %d\n",db.name,db.age)
	w.Write([]byte("json message"))
}


func main(){
	db:=DB{"doggy",27}
	mux := http.NewServeMux()
	mux.HandleFunc("/get",get)
	mux.HandleFunc("/list",db.list)
	http.ListenAndServe("localhost:1234",mux)
}

posted @   yvhqbat  阅读(350)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· 程序员常用高效实用工具推荐,办公效率提升利器!
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 【译】WinForms:分析一下(我用 Visual Basic 写的)
点击右上角即可分享
微信分享提示