1. http server

package main

import (
"fmt"
"net/http"
) func main() {
// 请求url和对应处理函数
http.HandleFunc("/hello", myHandleFunc) // 开启服务
http.ListenAndServe("127.0.0.1:8080", nil) } func myHandleFunc(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.RemoteAddr, "链接成功")
fmt.Println(r.Method, r.URL.Path)
fmt.Println(r.Header)
fmt.Println(r.Body)
// 回复
var buf = []byte("hello 马亚南, 这是服务器发送给你的消息")
w.Write(buf)
}

  

2. http client

package main

import (
"fmt"
"io"
"net/http"
) func main() {
// http客户端
resp, _ := http.Get("http://127.0.0.1:8080/hello")
defer resp.Body.Close() fmt.Println(resp.Status)
fmt.Println(resp.Header) buf := make([]byte, 1024) for {
// 接收服务端消息
n, err := resp.Body.Read(buf)
if err != nil && err != io.EOF {
fmt.Println(err)
return
} else {
fmt.Println("读取完毕")
ret := string(buf[:n])
fmt.Println(ret)
break
}
} }

  

golang中http编程的更多相关文章

  1. golang中的socket编程

    0.1.索引 https://waterflow.link/articles/1664591292871 1.tcp的3次握手(建立连接) 客户端的协议栈向服务器端发送了 SYN 包,并告诉服务器端当 ...

  2. Golang面向API编程-interface(接口)

    Golang面向API编程-interface(接口) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Golang并不是一种典型的面向对象编程(Object Oriented Pr ...

  3. 【荐】详解 golang 中的 interface 和 nil

    golang 的 nil 在概念上和其它语言的 null.None.nil.NULL一样,都指代零值或空值.nil 是预先说明的标识符,也即通常意义上的关键字.在 golang 中,nil 只能赋值给 ...

  4. [golang note] 网络编程 - RPC编程

    net包 • 官方文档 http://godoc.golangtc.com/pkg/net/ Package net provides a portable interface for network ...

  5. Golang 中的 面向对象: 方法, 类, 方法继承, 接口, 多态的简单描述与实现

    前言: Golang 相似与C语言, 基础语法与C基本一致,除了广受争议的 左花括号 必须与代码同行的问题, 别的基本差不多; 学会了C, 基本上万变不离其宗, 现在的高级语言身上都能看到C的影子; ...

  6. golang之面向对象编程

    1.Golang语言面向对象编程说明 1)Golang也支持面向对象编程(OOP),但是和传统的面向对象编程有区别,并不是纯粹的面向对象语言.所以我们说Golang支持面向对象编程特性是比较准确的. ...

  7. Golang的面向对象编程【结构体、方法、继承、接口】

    Golang也支持面向对象编程.但与以前学过传统的面向对象编程语言有区别.1)Golang没有类class,Go语言的结构体struct和类class有相似的特性.2)Golang中不存在继承,方法重 ...

  8. golang中的RPC开发-2

    RPC简介 远程过程调用(Remote Procedure Call,RPC)是一个计算机通信协议 该协议允许运行于一台计算机的程序调用另一台计算机的子程序,而程序员无需额外地为这个交互作用编程 如果 ...

  9. golang中的选项模式

    索引 https://waterflow.link/articles/1663835071801 当我在使用go-zero时,我看到了好多像下面这样的代码: ... type ( // RunOpti ...

随机推荐

  1. clickhouse配置登录密码

    执行 PASSWORD=$(base64 < /dev/urandom | head -c8); echo "123456"; echo -n "123456&qu ...

  2. centos7 升级php版本到7.2

    #自带的只有5.4版本 yum provides php [root@localhost etc]# yum provides php Loaded plugins: fastestmirror, l ...

  3. MySQL数据导入报错:Got a packet bigger than‘max_allowed_packet’bytes的问题

    修改my.cnf,需重启mysql. 在 [MySQLd] 部分添加一句(如果存在,调整其值就可以): max_allowed_packet=512M 查找MySql的配置文件my.cnf所在路径参考 ...

  4. Ubuntu Server服务器上架设Git Server服务器

    1.设置公钥 ubuntu:/home/git$ ssh-keygen -t rsa #生成密钥 这里会提示输入密码,我们不输入直接回车即可. 然后用刚生成公钥/home/git/.ssh/id_rs ...

  5. 【LeetCode】855. Exam Room 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/exam-roo ...

  6. 【LeetCode】290. Word Pattern 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】31. Next Permutation 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 逆序数字交换再翻转 库函数 日期 题目地址:http ...

  8. 1374 - Confusion in the Problemset

    1374 - Confusion in the Problemset    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory ...

  9. MongoDB应用场景及选型

    1. MongoDB数据库定位 * OLTP数据库 * 原则上Oracle和MySQL能做得事情,MongoDB都能做(包括ACID事务) * 优点:横向扩展能力,数据量或并发量增加时候可以自动扩展 ...

  10. 分别使用time 和 datetime模块记录当前时间

    工作中经常混淆这两种方法 现记录一下 加深印象 代码如下: >>> import time>>> import datetime>>> ct1 = ...