当前位置: 首页 > news >正文

上海网站建设品北京seo怎么优化

上海网站建设品,北京seo怎么优化,wordpress更换空间,北京网页设计公司兴田德润网址多少前情提要: 需要安装好go的环境和VSCode的go插件。 hello world快速上手 1、创建go.mod 在项目根目录下打开命令行,或者直接用VSCode中的终端。输入命令 go mod init github.com/solenovex/web-tutorial 然后就能看到项目结构中多了一个go.mod 2、…

前情提要:

需要安装好go的环境和VSCode的go插件。

hello world快速上手

1、创建go.mod

在项目根目录下打开命令行,或者直接用VSCode中的终端。输入命令

go mod init github.com/solenovex/web-tutorial

然后就能看到项目结构中多了一个go.mod

2、编写main.go

package mainimport 	"net/http"func main() {//HandleFunc有两个参数,第一个参数相当于一个路由地址。写“/”表示监听的根地址//第二个参数是个回调函数。函数内也有两个参数,第一个参数w是用来写响应的//第二个参数r会把传入请求的所有信息都包裹在里面。http.HandleFunc("/", func (w http.ResponseWriter, r *http.Request)  {w.Write([]byte("hello world"))})//监听请求的地址和端口 相当于一个路由器http.ListenAndServe("localhost:8080", nil)
}

3、go run main.go运行项目

在项目根目录下打开命令行,或者直接用VSCode中的终端。输入命令:

go run main.go

 然后打开浏览器输入相应地址,可以看到hello world。

http.Server:

http.Server 这是一个 struct,里面有若干字段。以下是常用字段

Addr 字段表示网络地址 如果为“”,那么就是所有网络接口的 80 端口

Handler 字段 如果为 nil,那么就是 DefaultServeMux ListenAndServe() 函数。

其封装好的语句为如下:

http.ListenAndServe("localhost:8080", nil)

底层源码如下:

server := http.Server{Addr:    "localhost:8080",Handler: nil,
}
server.ListenAndServe()

所以这两种代码是等效的,第一种更简洁,第二种灵活性更强。

handler:

handler 是一个接口(interface)

handler 里面只定义了一个方法 ServeHTTP()

参数一:HTTPResponseWriter 参数二:指向 Request 这个 struct 的指针  

源码:   type Handler interface {ServeHTTP(ResponseWriter, *Request)   }

package mainimport "net/http"type myHandler struct{}func (m *myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {w.Write([]byte("hello world"))
}func main() {mh := myHandler{}//以下五行就和http.ListenAndServe("localhost:8080", &mh)效果一样,是其源码。server := http.Server{Addr:    "localhost:8080",Handler: &mh,}server.ListenAndServe()}

http.Handle: 

该函数可以实现多个handler注册到DefaultServeMux上的效果,从而达到一个路径对应一个请求,一个处理逻辑:

package mainimport "net/http"type helloHandler struct{}func (m *helloHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {w.Write([]byte("hello world"))
}type aboutHandler struct{}func (m *aboutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {w.Write([]byte("about"))
}func main() {mh := helloHandler{}ah := aboutHandler{}server := http.Server{Addr:    "localhost:8080",Handler: nil,}http.Handle("/hello", &mh)http.Handle("/about", &ah)server.ListenAndServe()
}

 

 

http.HandleFunc

func HandleFunc(pattern string, handler func(ResponseWriter, *Request))

package mainimport "net/http"func welcome(w http.ResponseWriter, r *http.Request) {w.Write([]byte("welcome"))
}func main() {server := http.Server{Addr:    "localhost:8080",Handler: nil,}http.HandleFunc("/home", func(w http.ResponseWriter, r *http.Request) {w.Write([]byte("home"))})http.HandleFunc("/welcome", welcome)//相当于http.Handle("/welcome", http.HandlerFunc(welcome))server.ListenAndServe()
}

ps:Go 有一个函数类型:HandlerFunc。可以将某个具有适当签名的函数 f,适配成为一个 Handler,而这个 Handler 具有方法 f。

两者区别:


​​​​​​​

http://www.hkea.cn/news/170892/

相关文章:

  • 没有网站可以做seo排名吗小学生简短小新闻摘抄
  • 做程序网站需要什么代码宁波seo搜索排名优化
  • 网站建设开发语言新冠病毒最新消息
  • 怎么做1688网站网页制作工具有哪些
  • 一个网站的主题和设计风格最好用的免费建站平台
  • 网站开发主页手机优化游戏性能的软件
  • 怎么做属于自己的域名网站网络策划方案
  • destoon做的网站百度商务合作联系
  • 金山区网站制作网络营销策划书1500字
  • 厦门网站建设制作工具熊猫关键词挖掘工具
  • 徐州网站建设 网站推广百度首页快速排名系统
  • 在线转格式网站怎么做拼多多seo 优化软件
  • 成都理工疫情最新消息贵港seo
  • 网站如何防止攻击怎么自己做一个小程序
  • 企业网站建设英文百度收录
  • wordpress查版本sem和seo的区别
  • 网站设计说明书怎么写网站建设平台官网
  • 有建网站的软件阿里云域名注册万网
  • 站长工具排名分析怎么创建公司网站
  • 网站建设标书四川seo哪里有
  • 接网站开发做多少钱建一个外贸独立站大约多少钱
  • wordpress表单录入seo报告
  • python做网站显示表格星巴克seo网络推广
  • 一个com的网站多少钱管理微信软件
  • 蒙阴网站建设软文代写网
  • 用python做一旅游网站南昌seo计费管理
  • 湖北省建设厅win10优化软件哪个好
  • 湖南企业建站系统平台软文有哪些发布平台
  • 南通 网络 公司网站真正免费建站
  • 做图骂人的图片网站网络服务