一文搞懂使用Go语言快速搭建HTTP服务

简介: 一文搞懂使用Go语言快速搭建HTTP服务
+关注继续查看

第一个http程序:Hello World

package main

import "net/http"

//最简单的HTTP服务
func RunHttp1() {
   http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
      w.Write([]byte("Hello Http!"))
   })
   http.ListenAndServe(":8080", nil)
}

func main() {
   RunHttp1()
}

PostMan测试:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XAWs0Gpa-1639888646018)(一文搞懂Go快速搭建HTTP服务.assets/image-20211212103712537.png)]

一般请求

一般参数请求:

package main

import (
    "fmt"
    "net/http"
)

func GetMethod1() {
   http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
      //获取参数
      str := r.URL.Query().Get("str")
      fmt.Println("Get Method Str is " + str)
      w.Write([]byte("Hello Http Get!"))
   })
   http.ListenAndServe(":8080", nil)
}


func main() {
    GetMethod1()
}

测试:

在这里插入图片描述

表单请求

package main

import (
   "fmt"
   "net/http"
   "strconv"
)

type Student struct {
   Name string
   Age  int
}

func GetMethod2() {
   http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
      //获取参数
      name := r.URL.Query().Get("name")
      age := r.URL.Query().Get("age")
      //将string类型转为int类型
      ageStr, err := strconv.Atoi(age)
      if err != nil {
         fmt.Println("err...")
      }
      stu := Student{Name: name, Age: ageStr}
      fmt.Println("Get Method Str is ", stu)
      w.Write([]byte("Hello Http Get Form!"))
   })
   http.ListenAndServe(":8080", nil)
}

func main() {
   GetMethod2()
}

JSON格式的HTTP报文

package main

import (
   "encoding/json"
   "fmt"
   "io/ioutil"
   "net/http"
)

//自定义结构体 对应JSON结构体
type Student struct {
   Id   int64  `json:"id"`
   Name string `json:"name"`
   Age  int    `json:"age"`
}

func TakeJSON() {
   http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
      //获取请求方法
      fmt.Println("req method : ", r.Method)
      //获取请求题
      body, err := ioutil.ReadAll(r.Body)
      if err != nil {
         fmt.Printf("获取请求体错误 , %v\n", err)
         return
      }
      fmt.Println("请求体 :", string(body))
      //将请求的JSON转化为结构体
      var stu Student
      if err = json.Unmarshal(body, &stu); err != nil {
         fmt.Printf("反序列化失败 , %v\n", err)
         return
      }
      fmt.Printf("反序列化成功,JSON解析结果 %+v", stu)
      w.Write([]byte("Hello Http Get Form!"))
   })
   http.ListenAndServe(":8080", nil)
}

func main() {
   TakeJSON()
}
目录
相关文章
|
1月前
|
网络协议 Go
go的net/http有哪些值得关注的细节? 4
go的net/http有哪些值得关注的细节?
43 0
|
1月前
|
网络协议 Go
go的net/http有哪些值得关注的细节? 3
go的net/http有哪些值得关注的细节?
48 0
|
1月前
|
Go
go的net/http有哪些值得关注的细节? 2
go的net/http有哪些值得关注的细节?
27 0
|
1月前
|
网络协议 Go
go的net/http有哪些值得关注的细节? 1
go的net/http有哪些值得关注的细节?
26 0
|
1月前
|
Go
Go http包建立Web服务器
Go http包建立Web服务器
32 0
|
2月前
|
安全 网络协议 算法
GO 中如何设置 HTTPS 分享
先回顾一下上次说到关于HTTP相关的知识点 • HTTP 属于网络模型中的应用层协议 , 应用层的作用就是规定应用程序使用的用语规范
|
5月前
|
Go API
Golang:gin-gonic/gin一个用 Go (Golang) 编写的 HTTP Web 框架
Golang:gin-gonic/gin一个用 Go (Golang) 编写的 HTTP Web 框架
60 0
Golang:gin-gonic/gin一个用 Go (Golang) 编写的 HTTP Web 框架
|
7月前
|
Go 微服务
Go HTTP 调用(上)
本文介绍了在 Go 语言里如何进行 HTTP 调用,需要通过 http 包里的 Client 结构体变量,调用其方法 Do 进行 HTTP 调用,在 HTTP 调用前,需要通过 http 包里的 Request 结构体封装请求路径和请求参数。最后通过 GET 请求案例讲述了 query 参数和 header 参数如何设置,以及响应体的获取方法。
165 1
Go HTTP 调用(上)
|
7月前
|
JSON Go API
Go HTTP 调用(下)
本文通过 POST 请求,介绍了如何传递 json 类型和 application/x-www-form-urlencoded 类型的 body 参数。对于 HTTP 中的 query 参数和 body 参数的如何传递,上下两篇文章已经通过例子进行介绍。虽然举的例子是 GET 和 POST 请求,如果想要调用 PUT、DELETE 等请求,只需要在 NewRequestWithContext 函数中,指定第二个参数为 http.MethodPut、http.MethodDelete 等就行。
77 1
Go HTTP 调用(下)
|
7月前
|
关系型数据库 MySQL 中间件
使用 Go HTTP 框架 Hertz 进行 JWT 认证
上一篇文章简单介绍了一个高性能的 Go HTTP 框架——Hertz,本篇文章将围绕 Hertz 开源仓库的一个 demo,讲述如何使用 Hertz 完成 JWT 的认证与授权流程。
202 0
使用 Go HTTP 框架 Hertz 进行 JWT 认证
相关产品
云迁移中心
推荐文章
更多