首页 > 其他分享 >0147-Go-HTTP 服务端

0147-Go-HTTP 服务端

时间:2023-01-30 19:25:13浏览次数:51  
标签:http headers Go HTTP hello 服务端

环境

  • Time 2022-08-25
  • Go 1.19

前言

说明

参考:https://gobyexample.com/http-servers

目标

使用 Go 语言 HTTP 服务端。

示例

package main

import (
    "fmt"
    "net/http"
)

func hello(w http.ResponseWriter, req *http.Request) {

    fmt.Fprintf(w, "hello\n")
}

func headers(w http.ResponseWriter, req *http.Request) {

    for name, headers := range req.Header {
        for _, h := range headers {
            fmt.Fprintf(w, "%v: %v\n", name, h)
        }
    }
}

func main() {

    http.HandleFunc("/hello", hello)
    http.HandleFunc("/headers", headers)

    http.ListenAndServe(":8090", nil)
}

总结

使用 Go 语言 HTTP 服务端。

附录

标签:http,headers,Go,HTTP,hello,服务端
From: https://www.cnblogs.com/jiangbo4444/p/17077039.html

相关文章

  • 0148-Go-上下文
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/context目标使用Go语言的上下文。示例packagemainimport("fmt""net/http""time"......
  • 0149-Go-生成进程
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/spawning-processes目标使用Go语言生成进程。示例packagemainimport("fmt""......
  • 0150-Go-执行进程
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/execing-processes目标使用Go语言执行进程。示例packagemainimport("os""os......
  • 0151-Go-信号
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/signals目标使用Go语言的信号。示例packagemainimport( "fmt" "os" "os/signal" "......
  • 0152-Go-退出
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/exit目标退出Go程序。示例packagemainimport("fmt""os")funcmain(){......
  • 0138-Go-文件目录
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/directories目标使用Go语言处理文件目录。示例packagemainimport("fmt""os"......
  • 0139-Go-临时文件
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/temporary-files-and-directories目标使用Go语言处理临时文件。示例packagemainimport......
  • 0140-Go-内嵌命令
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/embed-directive目标使用Go语言的内嵌命令。示例packagemainimport("embed")/......
  • 0141-Go-单元测试
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/testing-and-benchmarking目标使用Go语言进行测试。示例packagemainimport("fmt"......
  • 0142-Go-命令行参数
    环境Time2022-08-25Go1.19前言说明参考:https://gobyexample.com/command-line-arguments目标使用Go语言的命令行参数。示例packagemainimport("fm......