Golang net.ListenTCP 结构

huangapple go评论148阅读模式
英文:

Golang net.ListenTCP Structure

问题

Golang函数net.ListenTCP的定义如下:

func ListenTCP(net string, laddr *TCPAddr)

它接受一个字符串参数,表示使用的TCP类型/版本,然后是一个TCPAddr结构体,用于定义要绑定的IP地址、使用的端口等信息。

然而,我经常看到这个函数被这样使用:

net.ListenTCP("tcp", ":8080")

如果第二个参数需要一个TCPAddr结构体/对象,为什么传递一个字符串也能工作呢?

英文:

The definition for the Golang function net.ListenTCP is:

func ListenTCP(net string, laddr *TCPAddr)

So it takes a string which is the type / version of TCP being used, and then a TCPAddr struct, which defines things like IP address to bind to, port to use etc.

However, I often see this function used like this:

net.ListenTCP("tcp", ":8080")

If the second parameter is looking for a TCPAddr struct / object, why does passing it a string work?

答案1

得分: 5

你正在将net.ListenTCPnet.Listen混淆。

func ListenTCP(net string, laddr *TCPAddr) (*TCPListener, error)

func Listen(net, laddr string) (Listener, error)
英文:

You are confusing net.ListenTCP with net.Listen.

func ListenTCP(net string, laddr *TCPAddr) (*TCPListener, error)

vs

func Listen(net, laddr string) (Listener, error)

huangapple
  • 本文由 发表于 2014年7月19日 04:42:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/24833470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定