码迷,mamicode.com
首页 > Web开发 > 详细

go 实现简单的http web服务

时间:2019-08-02 22:36:07      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:指定   实现   空字符串   mat   hand   nil   att   listen   main   

package main

import (
	"fmt"
	"net/http"
)

func hello(w http.ResponseWriter, r *http.Request) {
	fmt.Println("handle hello")
	fmt.Fprintf(w, "hello12345")
}

func index (w http.ResponseWriter, r *http.Request) {
	//Fprintf根据format参数生成格式化的字符串并写入w。返回写入的字节数和遇到的任何错误
	fmt.Fprintf(w, "this is index")
}

func main () {
	//HandleFunc注册一个处理器函数handler和对应的模式pattern
	//实现路由功能, hello 为函数名
	http.HandleFunc("/", hello)
	http.HandleFunc("/index", index)
	//ListenAndServe监听srv.Addr指定的TCP地址,并且会调用Serve方法接收到的连接。如果srv.Addr为空字符串,会使用":http"。
	err := http.ListenAndServe("0.0.0.0:8080", nil)

	if err != nil {
		fmt.Println("http listen failed")
	}

}

  

go 实现简单的http web服务

标签:指定   实现   空字符串   mat   hand   nil   att   listen   main   

原文地址:https://www.cnblogs.com/zhangxiaoj/p/11291374.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!