go学习:启动http服务

@Ta 2021-08-25 4974点击
package main

import (
	"log"
	"net/http"
)

// IndexHandler 首页路由处理函数
func IndexHandler(w http.ResponseWriter, req *http.Request) {
	// 响应 hello world
	_, err := w.Write([]byte("hello world"))

	if err != nil {
		log.Fatalln("listen err:", err)
	}
}


func main() {
	// 注册首页路由的处理函数
	http.HandleFunc("/", IndexHandler)

	// 监听端口启动服务
	err := http.ListenAndServe(":8089", nil)

	if err != nil {
		log.Fatalln("listen err:", err)
	}
}

回复列表(8|隐藏机器人聊天)
  • @Ta / 2021-08-25 / /
    大佬厉害ヾ(Ő∀Ő๑)ノ
    广告位一个草根小破站http://huue.cc乎学技术自制小尾巴
  • @Ta / 2021-08-25 / /
    学几天就有这个进展真是令在下佩服


    w http.ResponseWriter, req *http.Request

    不知道这是啥意思
  • @Ta / 2021-08-25 / /

    有望超过老虎
    黑屋屋👾

  • @Ta / 2021-08-25 / /
    @大尨,类似实例化用到的类
  • @Ta / 2021-08-25 / /
    @,是 http.ResponseWriter 赋值 给 变量 w ,*http.Request 赋值 给 变量 req 的意思吗?

    其次 *http.Request  这个 * 代表啥意思的?
  • @Ta / 2021-08-25 / /
    被禁言
    用户被禁言,发言自动屏蔽。
  • @Ta / 2021-08-26 / /
    @大尨,w是参数名称,http.ResponseWriter 是 http 响应的接口结构体,属于数据类型,也就是说他前面是参数,后面是参数类型

    而 req *http.Request 中 req 同样是参数名,*http.Request 则是 http.Request 的指针类型
  • @Ta / 2021-08-27 / /
    log.Fatalln不要在业务代码里面使用,实现是包含了panic,除非你用recover进行处理,不然一出错,程序直接就崩掉了
添加新回复
回复需要登录