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. java 依赖注入

    https://blog.csdn.net/coderder/article/details/51897721 前言 在软件工程领域,依赖注入(Dependency Injection)是用于实现控制 ...

  2. 树莓派4B踩坑指南 - (2)安装系统及初始化

    安装系统及初始化 格式化TF卡:SDFormatter 4.0.如果需要换系统,则必须先烧录进一个空img,然后再格式化! 烧录系统:Win32DiskImager-0.9.5 更改默认密码:账号pi ...

  3. 远程登陆ubantu服务器 .bashrc文件每次打开终端都需要source的问题

    通过创建的用户登录ubantu服务器时,.bashrc文件每次都要重新配置,要不然里面的配置如命令的简写如 ll 等就无法识别,本方法用于实现登录时自动执行.bashrc文件. 1.ubantu启动时 ...

  4. Python 基础之循环结构 while

    一.while循环介绍 while 循环 可以提高代码的效率,减少代码的冗余 while 条件表达式:    code1    code2如果条件表达式成立,返回Ture,就执行其中的代码块 1.基本 ...

  5. Python Web 框架原理

    Web Socket 所谓 Web 服务,本质上就是用户使用一个 socket 客户端(浏览器)去访问一个 socket 服务端. 下面是一个最基础的基于 socket 的 Python Web 服务 ...

  6. Intent的常用属性之ComponentName

    启动activity的另一种方式 在按钮中添加如下代码 ComponentName componentName=new ComponentName(MainActivity.this,NewActiv ...

  7. c++存储区域

    来自:https://www.cnblogs.com/simonote/articles/3146038.html 在C++中,内存分成5个区,他们分别是堆.栈.自由存储区.全局/静态存储区和常量存储 ...

  8. WC2020「Fantasie」

    由于某些不可抗拒因素,这篇文章鸽了

  9. tensorflow之逻辑回归模型实现

    前面一篇介绍了用tensorflow实现线性回归模型预测sklearn内置的波士顿房价,现在这一篇就记一下用逻辑回归分类sklearn提供的乳腺癌数据集,该数据集有569个样本,每个样本有30维,为二 ...

  10. SpringBoot Date类型插入数据库始终比正确时间早一天问题解决办法

    bug描述 昨天的Date插入不进去问题解决后,一直没发现其实插入的时间一直比正确的时间早一天 输出sql语句,发现insert语句还是对的,不知道为什么插入数据库之后结果就早了一天 https:// ...