Trying to understand how attaching ServeHTTP to a variable with int type works

xuanbao · · 625 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m going through a golang tutorial on udemy and there is an example of how to implement ServeHTTP. In the example below, why is it that we can attach ServeHTTP to the variable hotdog which is an integer? More concisely, what is the difference between implementing it on an int versus another type (i.e. string)?</p> <pre><code>type hotdog int func (m hotdog) ServeHTTP(res http.ResponseWriter, req *http.Request) { err := req.ParseForm() if err != nil { log.Fatalln(err) } </code></pre> <hr/>**评论:**<br/><br/>jerf: <pre><p>You aren&#39;t attaching it to an int, you&#39;re attaching it to a <code>hotdog</code>. You can implement methods on any type. The only reason you can&#39;t implement methods on <code>int</code> directly is that you can&#39;t implement methods on types that don&#39;t belong to your package.</p> <p>If it seems strange, don&#39;t think about it from an &#34;object oriented&#34; point of view; think about it from the memory perspective. When you use an object in another language, somewhere there&#39;s a pointer or something to the struct that is the members of the object. Well, who says that has to be a struct on the other end? Why not an int or a <code>chan string</code> or anything else?</p> <p>Ultimately, Go-style methods are just a fancy way of calling functions where the first parameter happens to be picked up from the thing in front of the dot. There are other languages where that is not an adequate summary, but it is in Go.</p></pre>TheMerovius: <pre><p>I believe you mean &#34;a fancy way of calling functions&#34; :)</p></pre>jerf: <pre><p>I believe I do. (Fixed.) You know what they say... if there&#39;s one thing tautologies are, it&#39;s tautological.</p></pre>fromagescratch: <pre><p>You are adding a method to the type hotdog. </p></pre>Guy-Lambo: <pre><p>I just realized this now after reading your comment this morning. This entire time I thought it was a variable declaration. Makes a bit more sense now that it&#39;s a hotdog type with underlying/base type int.</p></pre>fromagescratch: <pre><p>Glad it helped. Good luck on your tutorial and have fun learning!</p></pre>TheMerovius: <pre><p>In Go, any (defined) type can have methods, no matter what it&#39;s underlying type is. So, <code>type hotdog int</code> declares a new type with underlying type <code>int</code> and then you define a <code>ServeHTTP</code> method on it. From then on, you can call <code>ServeHTTP</code> on any value of type <code>hotdog</code>. Other language might have classes to define method, but Go doesn&#39;t make that distinction.</p> <blockquote> <p>More concisely, what is the difference between implementing it on an int versus another type (i.e. string)?</p> </blockquote> <p>There is none, from Go&#39;s perspective. <code>int</code>, <code>string</code>, <code>struct</code>s, pointers, <code>chan</code>s, <code>func</code>s, all can have methods declared on them. It&#39;s useful for example for <a href="https://godoc.org/net/http#HandlerFunc" rel="nofollow">http.HandlerFunc</a>. So, if your handler for some reason can be represented as an <code>int</code>, you can use this. If it can&#39;t use something else. :)</p></pre>whichpaul: <pre><p>As I understand it, there&#39;s practically no difference. If &#39;hotdog&#39; is &#39;type hotdog int&#39; then the ServeHTTP function you have defined is basically handed an extra parameter &#39;m hotdog&#39;. This language feature is helpful for at least two reasons. Firstly, it enables you to implement interfaces on all sorts of data types, such as the http.Handler interface on &#39;type hotdog int&#39;. Secondly, as in the example, it enables you to use the most efficient data type necessary for the job. So, implementing this function on a string would work the same way, but perhaps the underlying data type may be able to be used by different functions or interfaces than an integer.</p> <p>Hope that helps.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

625 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传