httpSvr

// HandleFunc registers the handler function for the given pattern
// in the DefaultServeMux.
// The documentation for ServeMux explains how patterns are matched.
func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {
DefaultServeMux.HandleFunc(pattern, handler)
}

  

// ListenAndServe listens on the TCP network address addr and then calls
// Serve with handler to handle requests on incoming connections.
// Accepted connections are configured to enable TCP keep-alives.
//
// The handler is typically nil, in which case the DefaultServeMux is used.
//
// ListenAndServe always returns a non-nil error.
func ListenAndServe(addr string, handler Handler) error {
server := &Server{Addr: addr, Handler: handler}
return server.ListenAndServe()
}

  

package main

import (
"fmt"
"net/http"
) func Hello(w http.ResponseWriter, r *http.Request) {
fmt.Println("handle hello")
fmt.Fprintf(w, "dashboard page")
} func Login(w http.ResponseWriter, r *http.Request) {
fmt.Println("handle login")
fmt.Fprintf(w, "login page")
} func main() {
http.HandleFunc("/", Hello)
http.HandleFunc("/login/", Login)
err := http.ListenAndServe("127.0.0.1:8000", nil)
if err != nil {
fmt.Println("httpSvr listen failed")
}
}

  

httpClient

package main

import (
"fmt"
"io/ioutil"
"net/http"
) func main() {
res,err := http.Get("http://127.0.0.1:8000/")
if err != nil {
fmt.Println("get err:",err)
return
} data,err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println("get data err:",err)
return
}
fmt.Println(string(data))
}

  

go_http的更多相关文章

  1. golang net/http包

    http协议 超文本传输协议(HTTP,HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络传输协议,所有的WWW文件都必须遵守这个标准.设计HTTP最初的目的是为 ...

随机推荐

  1. Python流程控制-3 循环控制

    循环控制,就是让程序循环运行某一段代码直到满足退出的条件,才退出循环. Python用关键字for和while来进行循环控制,但是没有其它语言的do...while语句(在Java和PHP中都有do ...

  2. Mac安装navigate premium

    转自Navicat Premium for Mac v12.0.22.0 破解教程,macOS上手动破解,无需补丁,无毒今天换了电脑,想装一个Navicat,突然发现了这篇破解教程,竟爱不释手,顾Co ...

  3. 执行SQL时出现: ORDER BY clause is not in GROUP BY clause and contains nonaggregated c

    注意: 采用navicat新建数据库时,需要将编码方式设置为,字符集:utf8 -- UTF-8 Unicode ,排序规则:utf8_general_ci 在运行sql语句时,出现以下问题: [Er ...

  4. Guava LoadingCache不能缓存null值

    测试的时候发现项目中的LoadingCache没有刷新,但是明明调用了refresh方法了.后来发现LoadingCache是不支持缓存null值的,如果load回调方法返回null,则在get的时候 ...

  5. 怎样把exe程序注册成系统服务

    怎样把exe程序注册成系统服务 最近一段时间我们公司开发一款新的产品,要在服务器上运行一个服务端程序,为了方便我就希望能将这个程序注册成系统服务开机自动启动而不用每次重启系统都要手动启动程序.要实现这 ...

  6. axios发送post请求node服务器无法通过req.body获取参数

    问题: 项目前端使用Vue框架,后端使用node.js搭建本地服务器.前端通过 axios 方式请求后端数据的过程中,发现如果是 get 请求,服务器端能够通过 req.query 获取前端传递的参数 ...

  7. 第2节 storm实时看板案例:12、实时看板综合案例代码完善;13、今日课程总结

    详见代码 将任务提交到集群上面去运行 apache-storm-1.1.1/bin/storm jar cn.itcast.storm.kafkaAndStorm.KafkTopology kafka ...

  8. ch4 圆角框

    固定宽度的圆角框 只需要两个图像:一个应用于框的顶部,一个应用于底部 <div class="box"> <h2>Lorem Ipsum</h2> ...

  9. Java HotSpot(TM) Client VM 与 server VM 的配置

    在Linux 6.5 下安装Elasticsearch 出现错误: JVM is using the client VM [Java HotSpot(TM) Client VM] but should ...

  10. 关于c++ 感想

    前言 在学校开展了c++的课程,但是不得不说相当乏味. 原因很简单: 1.感觉c++很高级,自己就这智商怎么学的会哦,自己给了自己门槛. 2.c++很难快速的做出一个能够展现的项目,缺乏成就感. 3. ...