iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > GO >能否同时运行 http.ListenAndServe() 和 ReadFromUDP() 函数?
  • 359
分享到

能否同时运行 http.ListenAndServe() 和 ReadFromUDP() 函数?

2024-04-04 23:04:22 359人浏览 泡泡鱼
摘要

本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《能否同时运行 Http.ListenAndServe() 和 ReadFromUDP() 函数?》对你有很大帮助

本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《能否同时运行 Http.ListenAndServe() 和 ReadFromUDP() 函数?》对你有很大帮助!欢迎收藏,分享给更多的需要的朋友学习~

问题内容

我正在尝试编写一个简单的 WEB 应用程序来侦听 udp 数据包。

但是我要么只监听 udp 数据包,要么运行 web 应用程序... 我不熟悉 golang,但这是我用来...的代码 监听udp:

serverconn, _ := net.listenudp("udp", &net.udpaddr{ip:[]byte{#,#,#,#},port:####,zone:""})
  defer serverconn.close()
  buf := make([]byte, 1024)

  for {
    n, addr, _ := serverconn.readfromudp(buf)
    fmt.println("received ", string(buf[0:n]), " from ", addr)
  }

服务器逻辑:

package main
we import 4 important libraries 
1. “net/http” to access the core Go http functionality
2. “fmt” for fORMatting our text
3. “html/template” a library that allows us to interact with our html file.
4. "time" - a library for working with date and time.
import (
   "net/http"
   "fmt"
   "time"
   "html/template"
)

//create a struct that holds information to be displayed in our html file
type welcome struct {
   name string
   time string
}

//go application entrypoint
func main() {
   //instantiate a welcome struct object and pass in some random information. 
   //we shall get the name of the user as a query parameter from the url
   welcome := welcome{"anonymous", time.now().format(time.stamp)}

   //we tell go exactly where we can find our html file. we ask go to parse the html file (notice
   // the relative path). we wrap it in a call to template.must() which handles any errors and halts if there are fatal errors

   templates := template.must(template.parsefiles("templates/welcome-template.html"))

   //our html comes with CSS that go needs to provide when we run the app. here we tell go to create
   // a handle that looks in the static directory, go then uses the "/static/" as a url that our
   //html can refer to when looking for our css and other files. 

   http.handle("/static/", //final url can be anything
      http.stripprefix("/static/",
         http.fileserver(http.dir("static")))) //go looks in the relative "static" directory first using http.fileserver(), then matches it to a
         //url of our choice as shown in http.handle("/static/"). this url is what we need when referencing our css files
         //once the server begins. our html code would therefore be <link rel="stylesheet"  href="/static/stylesheet/...">
         //it is important to note the url in http.handle can be whatever we like, so long as we are consistent.

   //this method takes in the url path "/" and a function that takes in a response writer, and a http request.
   http.handlefunc("/" , func(w http.responsewriter, r *http.request) {

      //takes the name from the url query e.g ?name=martin, will set welcome.name = martin.
      if name := r.formvalue("name"); name != "" {
         welcome.name = name;
      }
      //if errors show an internal server error message
      //i also pass the welcome struct to the welcome-template.html file.
      if err := templates.executetemplate(w, "welcome-template.html", welcome); err != nil {
         http.error(w, err.error(), http.statusinternalservererror)
      }
   })

   //start the web server, set the port to listen to 8080. without a path it assumes localhost
   //print any errors from starting the webserver using fmt
   fmt.println("listening");
   fmt.println(http.listenandserve(":8080", nil));
}

取自(https://medium.com/google-cloud/building-a-go-web-app-from-scratch-to-deploying-on-google-cloud-part-1-building-a-简单-go-aee452a2e654)

我尝试将这两个提取物放入 1 个文件中,并使用

同时运行 2 个文件
go run *.go

如有任何帮助,我们将不胜感激!


解决方案


您将需要开始研究 goroutine - 因为您要求同时做两件事。我建议对通道、goroutines 和并发进行一些阅读:)

今天关于《能否同时运行 http.ListenAndServe() 和 ReadFromUDP() 函数?》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注编程网公众号!

您可能感兴趣的文档:

--结束END--

本文标题: 能否同时运行 http.ListenAndServe() 和 ReadFromUDP() 函数?

本文链接: https://www.lsjlt.com/news/595815.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • 能否同时运行 http.ListenAndServe() 和 ReadFromUDP() 函数?
    本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《能否同时运行 http.ListenAndServe() 和 ReadFromUDP() 函数?》对你有很大帮助...
    99+
    2024-04-04
  • python怎么同时运行多个函数
    在python同时运行多个函数,具体方法如下:def fun1():while True:time.sleep(2)print("fun1")def fun2():while True:time.sleep(6)print("fun2")t...
    99+
    2024-04-02
  • python如何同时运行两个函数
    在python中利用线程同时运行两个函数,具体方法如下:def fun1():while True:time.sleep(2)print("fun1")def fun2():while True:time.sleep(6)print("fu...
    99+
    2024-04-02
  • python性能检测工具函数运行内存及运行时间
    目录1、memory_profiler进程监视2、timeit 时间使用情况3、line_profiler行代码运行时间检测4、heartrate可视化检测工具前言: python虽...
    99+
    2024-04-02
  • Bash 能否实时运行在 PHP 中打包?
    Bash 和 PHP 都是广泛使用的编程语言,Bash 是一种 Unix shell,用于命令行脚本编写,而 PHP 是一种服务器端脚本语言,用于 Web 应用程序开发。对于开发人员而言,这两种语言都具有各自的优势和特点,但是否可以将 B...
    99+
    2023-09-15
    打包 bash 实时
  • 如何同时运行 Java 函数和 Laravel,让你的应用更加强大?
    在现代开发中,很多应用都需要同时使用不同的编程语言和框架。Java 和 Laravel 是两种非常流行的技术,它们分别用于开发后端应用和 web 应用程序。本文将介绍如何同时运行 Java 函数和 Laravel,以加强您的应用程序。 什么...
    99+
    2023-10-25
    函数 laravel 同步
  • Python 和 Django 能否同时实现实时打包的功能?
    随着互联网的快速发展,实时性已经成为了许多应用的核心要求之一。在这样的背景下,许多开发人员开始寻求一种能够同时实现实时性和打包的技术方案,以满足各种应用场景的需求。而 Python 和 Django 作为目前最为流行的开发语言和 Web ...
    99+
    2023-08-29
    django 实时 打包
  • Java程序打包后能否在不同容器中运行?
    Java程序打包后能否在不同容器中运行? 在Java开发中,我们经常需要将程序打包成可执行的Jar包或者War包来进行部署。但是,当我们需要将程序部署到不同的容器中时,我们就会面临一个问题:Java程序打包后能否在不同容器中运行? 答案是肯...
    99+
    2023-07-27
    unix 打包 容器
  • Linux系统中,如何同时运行ASP和Spring?
    随着互联网的快速发展,现在很多企业和个人都需要在网站中使用多个不同的技术框架。在这种情况下,如何同时运行ASP和Spring呢?本文将为大家详细介绍。 安装必要的软件 在Linux系统中同时运行ASP和Spring,需要安装一些必要的...
    99+
    2023-08-29
    spring 同步 linux
  • python函数运行内存时间等性能检测工具
    目录基础测试函数memory_profiler进程timeit 时间使用情况line_profiler行代码检测heartrate可视化检测python虽然是一门'慢语言&#...
    99+
    2024-04-02
  • chatgpt赋能python:如何同时运行两个Python代码
    如何同时运行两个Python代码 Python是一种广泛使用的高级编程语言,广泛应用于数据科学、人工智能、网络开发等领域。在学习和使用Python时,我们经常需要同时运行多个代码文件。本文将介绍如何使...
    99+
    2023-09-14
    python chatgpt 开发语言 计算机
  • 在Linux系统中,如何同时运行ASP和Spring?
    随着互联网技术的发展,越来越多的Web开发者开始使用ASP和Spring作为自己的Web开发框架。ASP是一种基于Microsoft Windows的Web应用程序框架,而Spring则是一种基于Java的应用程序框架。在Linux系统中...
    99+
    2023-08-29
    spring 同步 linux
  • npm 包的实时同步功能在 ASP 中是否可行?
    随着 Node.js 的普及,npm 包已经成为了开发者们最常用的工具之一。npm 包的方便和易用性使得它们在 ASP.NET 开发中也越来越受欢迎。然而,有一个问题一直困扰着 ASP.NET 开发者: 首先,我们来了解一下 npm 包的实...
    99+
    2023-10-21
    npm 同步 实时
  • JavaScript时间函数和数学运算函数有哪些
    今天小编给大家分享一下JavaScript时间函数和数学运算函数有哪些的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,...
    99+
    2024-04-02
  • C++ 函数库如何进行计时和性能分析?
    在 c++++ 中进行计时和性能分析可以通过使用计时函数库,如 <chrono> 和 来测量代码片段的执行时间。实战中,我们可以使用 <chrono> 函数库测...
    99+
    2024-04-18
    性能分析 计时 c++ 标准库
  • python如何快速计算函数运行时间
    这篇文章主要介绍python如何快速计算函数运行时间,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!如何快速计算函数运行时间计算一个函数的运行时间,你可能会这样子做import time  ...
    99+
    2023-06-27
  • 能否同时使用PHP缓存和JavaScript实时加速您的网站?
    随着互联网的发展,网站的速度越来越受到重视。快速的加载速度不仅可以提高用户体验,还可以提高搜索引擎排名。在提高网站速度的过程中,使用缓存和JavaScript实时加速是两种常见的方法。那么,能否同时使用PHP缓存和JavaScript实时加...
    99+
    2023-09-22
    缓存 javascript 实时
  • python函数运行内存时间等性能检测工具怎么用
    这篇“python函数运行内存时间等性能检测工具怎么用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“python函数运行内存...
    99+
    2023-06-30
  • 如何让Java函数在打包时同步进行?
    在Java开发中,我们经常会遇到需要进行函数同步的情况。特别是在打包时,由于需要处理大量的代码和数据,函数同步就显得尤为重要。本文将介绍如何让Java函数在打包时同步进行。 一、什么是函数同步? 函数同步是指在多线程环境下,通过某种方式保证...
    99+
    2023-09-29
    打包 同步 函数
  • python中哪个函数可以记录运行时间
    在python中记录程序的运行时间,具体方法如下:使用time函数import timetime_start=time.time()time_end=time.time()print('Running time:{} seconds'.fo...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作