golange 开启http server 服务之后,怎么关闭呢?

-----------------------------------------------------------------------------------------------------

这个也不错: http://blog.csdn.net/codyguo/article/details/54582453

package main

import (
"log"
"io"
"time"
"net/http"
) func startHttpServer() *http.Server {
srv := &http.Server{Addr: ":8080"} http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "hello world\n")
}) go func() {
if err := srv.ListenAndServe(); err != nil {
// cannot panic, because this probably is an intentional close
log.Printf("Httpserver: ListenAndServe() error: %s", err)
}
}() // returning reference so caller can call Shutdown()
return srv
} func main() {
log.Printf("main: starting HTTP server") srv := startHttpServer() log.Printf("main: serving for 10 seconds") time.Sleep( * time.Second) log.Printf("main: stopping HTTP server") // now close the server gracefully ("shutdown")
// timeout could be given instead of nil as a https://golang.org/pkg/context/
if err := srv.Shutdown(nil); err != nil {
panic(err) // failure/timeout shutting down the server gracefully
} log.Printf("main: done. exiting")
}

golang中关闭http server的更多相关文章

  1. springboot中关闭eureka server中已注册服务列表自我保护配置

    配置集群服务可以向eureka通知应用是否可以使用a.在eureka server的application.properties中加入:# 设为false,关闭自我保护eureka.server.en ...

  2. Windows Server 2008中关闭事件跟踪程序的方法

    Windows Server 2008跟Windows Server 2003一样,在关机的时候会弹出一个“关闭事件跟踪程序”窗口,当然微软这么做是处于安全的考虑啦,但是如果我们只是个人用用的话,那就 ...

  3. 在系统启动时,Windows Vista 中、 在 Windows 7 中,Windows Server 2008 中和在 Windows Server 2008 R2 中的 497 天后未关闭 TIME_WAIT 状态的所有 TCP/IP 端口

    在系统启动时,Windows Vista 中. 在 Windows 7 中,Windows Server 2008 中和在 Windows Server 2008 R2 中的 497 天后未关闭 TI ...

  4. Go实战--golang中使用JWT(JSON Web Token)

    http://blog.csdn.net/wangshubo1989/article/details/74529333 之前写过关于golang中如何使用cookie的博客: 实战–go中使用cook ...

  5. [译]Golang中的优雅重启

    原文 Graceful Restart in Golang 作者 grisha 声明:本文目的仅仅作为个人mark,所以在翻译的过程中参杂了自己的思想甚至改变了部分内容,其中有下划线的文字为译者添加. ...

  6. python golang中grpc 使用示例代码详解

    python 1.使用前准备,安装这三个库 pip install grpcio pip install protobuf pip install grpcio_tools 2.建立一个proto文件 ...

  7. golang中的socket编程

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

  8. 在Windows Server 2012 R2中搭建SQL Server 2012故障转移集群

    需要说明的是我们搭建的SQL Server故障转移集群(SQL Server Failover Cluster)是可用性集群,而不是负载均衡集群,其目的是为了保证服务的连续性和可用性,而不是为了提高服 ...

  9. 基础知识 - Golang 中的正则表达式

    ------------------------------------------------------------ Golang中的正则表达式 ------------------------- ...

随机推荐

  1. python mail

    转载一个不错python mail封装 #!/usr/bin/python from email.MIMEText import MIMEText from email.MIMEMultipart i ...

  2. swal用法

    swal({   title: "确认删除?",   text: "Your will not be able to recover this imaginary fil ...

  3. python照相机模型与增强现实

    这次试验主要实现以平面和标记物进行姿态估计以及增强现实的应用. 一.以平面和标记物进行姿态估计(1)下面演示的是一个简单例子:如何在一副图像上放置一个立方体,原图如下: (2)先提取两幅JPG图像的S ...

  4. Vue的模板语法

    基本语法 <body> <script src="vue.js"></script> <div id="app"> ...

  5. sh NonUniqueObjectException

    话题引入: 使用hibernate进行更新操作时,首先调用了findById方法获取要修改的对象,此时session没有被关闭,接着重新创建一个对象,将要修改的属性值赋值给这个对象.调用修改方法抛出如 ...

  6. C#通过post发送接收数据流

    发送数据流方法 /// <summary> /// /// </summary> /// <param name="url">目标url< ...

  7. Bitmap.createBitmap函数有6个重载方法

    位图剪切参考重载方法4和6,重载方法6比较简单 public static Bitmap createBitmap (Bitmap src)从原位图src复制出一个新的位图,和原始位图相同 publi ...

  8. mysql主从同步,主库宕机解决方案

    链接:https://blog.csdn.net/zfl589778/article/details/51441719

  9. leetcode-240搜索二维矩阵II

    搜索二维矩阵II class Solution: def searchMatrix(self, matrix, target): """ :type matrix: Li ...

  10. MySQL数据库初识

    认识数据库 1 什么是数据(Data) 描述事物的符号记录称为数据,描述事物的符号既可以是数字,也可以是文字.图片,图像.声音.语言等,数据由多种表现形式,它们都可以经过数字化后存入计算机 在计算机中 ...