golang web服务器

The Go programming language is well-known for having a built-in web server. In this article you will learn how you can easily make your own web server with Go. You won’t need any other packages beside the ones that are already built in!

Go编程语言因具有内置的Web服务器而闻名。 在本文中,您将学习如何使用Go轻松制作自己的Web服务器。 除了已经内置的软件包之外,您不需要任何其他软件包!

First, hop in to your text editor. Then create a file called webserver.go and enter the following code:

首先,跳至您的文本编辑器。 然后创建一个名为webserver.go的文件并输入以下代码:

package mainimport ("net/http""io"
)func main() {http.HandleFunc("/", servePage)http.ListenAndServe(":8080", nil)
}func servePage(writer http.ResponseWriter, reqest *http.Request) {io.WriteString(writer, "Hello world!")
}

Let’s break down the block of code above. We import the net/http package: this package contains the web server itself. Then we also import the io package, we will make use of this later to actually serve something to the client.

让我们分解上面的代码块。 我们导入net/http包:该包包含Web服务器本身。 然后,我们还导入了io包,稍后我们将利用此包向客户端实际提供服务。

In the main function we do two things. First of all we instruct the server to let the function called servePage handle all incoming traffic to / - in this case it means that it handles requests to any URL.

main函数中,我们做两件事。 首先,我们指示服务器让称为servePage的函数处理/所有传入流量,在这种情况下,这意味着它处理对任何 URL请求。

The second thing we do is actually activating the server. We do this using a function named ListenAndServe. This function requires two parameters: the port (as string), in this case it’s 8080, and the handler (as Handler) - however the last one isn’t important yet. We will just make it nil and everything will work just fine.

我们要做的第二件事实际上是激活服务器。 我们使用名为ListenAndServe的函数进行此操作。 此函数需要两个参数: port (如string )(在本例中为8080 )和handler (如Handler )-但是最后一个并不重要。 我们将其设置nil ,一切都会正常进行。

In the servePage we do just one simple thing, for now. Using the io package and the WriteString function that it contains we can respond to the clients’ request with the text Hello world! (or any other string, of course).

目前,在servePage我们只做一件简单的事情。 使用io包及其包含的WriteString函数,我们可以使用文本Hello world!来响应客户的请求Hello world! (或者其他任何字符串)。

You also might have noticed that the servePage function has two arguments: the writer and the request. With the writer you can actually respond to a HTTP request and with the request you may get more information about the request itself.

您可能还已经注意到servePage函数具有两个参数: writerrequest 。 使用writer,您实际上可以响应HTTP请求,并且使用该request您可以获得有关请求本身的更多信息。

Congratulations! You just created your first web server! If you want to test it: just run go run webserver.go, fire up a browser and navigate to http://localhost:8080!

恭喜你! 您刚刚创建了第一个Web服务器! 如果要测试:只需运行go run webserver.go ,启动浏览器并导航到http://localhost:8080

翻译自: https://www.freecodecamp.org/news/how-to-make-your-own-web-server-with-go/

golang web服务器

golang web服务器_使用Go制作自己的Web服务器:快速指南相关推荐

  1. android socket 简易聊天室 java服务器_利用Socket制作一个简易的Android聊天室

    首先制作一个客户端,界面如下: 使用方法:启动后,首先在登录编辑框输入一个昵称,然后点击登录,上面灰色区域是聊天窗,其中会显示你的登录提示,显示其他人发的消息.在的登录成功后,可以在下面的发送编辑框内 ...

  2. ubuntu ftp服务器_如何在Ubuntu上安装FTP服务器?

    ubuntu ftp服务器 In this tutorial, let's learn how to install FTP server on Ubuntu. FTP or File Transfe ...

  3. java web服务_将Java服务公开为Web服务

    java web服务 本教程解决了开发人员面临的最实际的情况. 大多数时候,我们可能需要将某些现有服务公开为Web服务. 在项目生命周期的不同阶段可能会遇到这种情况. 如果这是初始阶段,那么您几乎是安 ...

  4. spring boot web项目_阿里技术专家带你使用Spring框架快速搭建Web工程项目

    点击上方 "程序员小乐"关注, 星标或置顶一起成长 第一时间与你相约 每日英文 We all have a past. It's how you deal with it. 每个人 ...

  5. linux搭建mcpe服务器_技术|如何搭建“我的世界”服务器

    我们将通过一个一步步的.新手友好的教程来向你展示如何搭建一个"我的世界(Minecraft)"服务器.这将会是一个长期的多人游戏服务器,你可以与来自世界各地的朋友们一起玩,而不用在 ...

  6. java osgi web开发_基于 OSGi 和 Spring 开发 Web 应用

    开发一个简单的OSGi Web应用实例 一个简单的Web应用 我们写一个简单的 Web 应用 compute.html :计算两个数字的和或乘积.如下图所示: 图1.一个简单例子 一个简单例子.bmp ...

  7. 磐石云服务器_磐石云爆款高防云服务器3年仅699元 限量100台 BGP线路 1核2G

    磐石云,国人商家,成立于2017年,主打高防云服务器产品,并且保证分配峰值内打死退款,所以是绝对真实的高防服务器产品.磐石云投稿活动蜗牛有段时间没有发布了,主要是他们的促销理念蜗牛不太喜欢,总是搞些什 ...

  8. 网易倩女找不到服务器,新倩女幽魂9月21日一直正在连接服务器_新倩女幽魂9月21日服务器一直连接解决办法_玩游戏网...

    明天<新倩女幽魂>就要和大家见面了,相信很多的网友已经迫不及待想在游戏中和居老师来一场偶遇.小编相信明天<新倩女幽魂>服务器一旦开启,一定会有大量的网友涌入,所以一定会有很多的 ...

  9. python web 文件管理_我的第一个python web开发框架(23)——代码版本控制管理与接口文档...

    书接上一回,小白和老菜聊到代码的版本控制和接口文档 小白:为什么要做版本控制,我不弄版本控制不也完成了项目了吗?要做版本控制不是很麻烦,又要安装服务又要提交代码,代码又不是多人用开发,还要写文档... ...

最新文章

  1. 关于Nikon Ai AF 28mm F1.4D遮光罩的问题
  2. mybais传入多个参数的方法
  3. java学习(165):inetaddress和inetsocketaddress
  4. 优先队列priority_queue的使用方式
  5. 今天中午过的不爽的原因分析
  6. 二级VB培训笔记11:培训答疑集
  7. 剑指 Offer II 028. 数组中出现次数超过一半的数字
  8. 6Y叔的clusterProfiler-book阅读 Chapter 6 KEGG analysis
  9. 读大师的书 说自己的话——《传世经典书丛评注版》邀你来点评
  10. Instruments-Automation: 通过命令行执行测试用例
  11. 与矩阵的秩有关的结论
  12. Unity3D FingerGestures 手势插件使用
  13. KALI 2020.4 搭建双网卡渗透环境
  14. chrome 自动加载ajax.googleapis,使用Redirector插件解决googleapis公共库加载的问题
  15. 【剑指offer】一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
  16. 不可思议:99%的人不了解的真实中国历史
  17. Excel 中如何去掉数值中出现的 E +,网上的方法都试过了不管用,改变单元格格式等都无效
  18. 记Oracle regexp_substr 一拆多查询缓慢sql优化
  19. 鸿蒙窍做何解释,终朝睡在鸿蒙窍 一任时人牛马呼
  20. Fast-MoCo: Boost Momentum-based Contrastive Learning with Combinatorial Patches

热门文章

  1. 【今日CV 视觉论文速览】19 Feb 2019
  2. Ubuntu 16.04 使用useradd添加用户后没有家目录的解决方法
  3. linux-mysql了解
  4. linux-history历史命令-光标的移动操作-命令行上的字符删除操作
  5. squid 出错页面GMT时间修改(FreeBSD)
  6. 【LeetCode】12. Integer to Roman 整型数转罗马数
  7. LB Cluster 之一:集群及LVS基础理论详解
  8. 谈谈JS里的{ }大括号和[ ]中括号的用法
  9. 了解.Net Remoting从基础开始
  10. 更灵活的定位内存地址的方法02 - 零基础入门学习汇编语言33