<p>Code: <a href="https://play.golang.org/p/s2KtkFrQs7R" rel="nofollow">https://play.golang.org/p/s2KtkFrQs7R</a></p>
<p>I would like to create multiple Dialers (20-30 different resolvers) using a list of DNS resolvers like
['8.8.8.8', '8.8.4.4']</p>
<p>But how can I do that without repeating the code of GoogleDNSDialer</p>
<hr/>**评论:**<br/><br/>gargamelus: <pre><p>You are looking for what is called a closure. Golang has first class functions, which means that functions can be passed as arguments to other functions, and returned by other functions.</p>
<p>For your task, you can make a function that constructs the dial function based on the DNS server IP address, and returns that function.</p>
<p>I tried to modify your code as little as possible to give you an idea, but did not test:
<a href="https://play.golang.org/p/49pAD1wY2qf" rel="nofollow">https://play.golang.org/p/49pAD1wY2qf</a></p></pre>zeromint: <pre><p>Thanks a lot! It solved my issue.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传