GO Web编程---网上书店(1)

本文介绍了使用Go语言进行Web编程,构建网上书店的第一步。主要内容包括安装Go环境,使用Goland作为IDE,设置go mod,导入html模板,并详细解释了`main.go`中的关键代码,如处理器函数和HTTP路由配置。通过这些步骤,实现了访问/main路由时显示网页并正确处理静态资源的展示。
摘要由CSDN通过智能技术生成

完成项目后的目录结构:
在这里插入图片描述
完成项目后的源码:

package main

import (
	"fmt"
	"html/template"
	"net/http"
)

func IndexHandler(w http.ResponseWriter, r *http.Request) {
   
	t := template.Must(template.ParseFiles("views/index.html"))
	t.Execute(w, "")
}
func main() {
   
	http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("views/static"))))
	http.Handle("/pages/", http.StripPrefix("/pages/", http.FileServer(http.Dir("views/pages"))))

	http.HandleFunc("/main", IndexHandler)
	fmt.Println("在监听:http://localhost:8080")
	err := http.ListenAndServe(":8080", nil)
	
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值