New to go, have a few questions to jump start the learning process.

xuanbao · · 797 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I am new to go and have a bunch of questions to get myself off the ground and running! </p> <p>Currently my dev circle is severely lacking in go developers and more importantly someone who knows good go practices. I want to take the initiative and be the chosen one.</p> <p>I&#39;ve done a few trivial go programs and went through the &#34;A Tour of Go&#34; examples. I&#39;ve read a few go gotcha threads and go articles here and there, and watched a pretty awesome video on go concurrency.</p> <p><strong>Quick background:</strong></p> <ul> <li>Most recently (for 7 years), I have been developing in python.</li> <li>My editor of choice is nano on linux and notepad on windows. (I know I know. I get grief almost every day.)</li> <li>Printing to the console is my debugging </li> <li>Introspection is how I learn/find new things</li> </ul> <p><strong>Questions:</strong></p> <ul> <li><p>Is there a good introspection feature (like dir() in python)? I do not want something like tab complete in my editor, I just want a way to see all the features an object from time to time. fmt.Println an object seems to give the values but not the names that go with them, maybe I am missing something.</p></li> <li><p>How are you supposed to find what file functions are within? I was looking at a few larger projects and it felt daunting to search through all the files in the folder for any given package to find the right file that housed the function that was being called. Is it just based on naming conventions or is there something that points to the specific file (kind of like in python)?</p></li> <li><p>What are the &#34;best&#34; handful of blogs/posts/vids for learning, using, and best practices for go? I will start checking out the &#34;Resources for new Go Programmers&#34; on the sidebar.</p></li> <li><p>Is there a package manager that lets you pull down projects for snap-able pieces (like pip)?</p></li> <li><p>Is there a multi environment tool that lets you have different versions of go libraries (like virtualenv)?</p></li> <li><p>Is the convention of returning an error value in (almost) every function and then checking if there was an error in the calling function standard practice? I <em>believe</em> I understand that there is no try/except in go and only to use panic when you really want things to bubble up.</p></li> </ul> <p>I am sure I will think of more questions but I think for now answers to these should get me moving pretty quickly!</p> <hr/>**评论:**<br/><br/>DeedleFake: <pre><p>Welcome to Go. I don&#39;t know Python as well as I should, but I&#39;ll try to answer your questions. Hopefully someone will correct what I inevitably get wrong.</p> <blockquote> <p>Is there a good introspection feature (like dir() in python)? I do not want something like tab complete in my editor, I just want a way to see all the features an object from time to time. fmt.Println an object seems to give the values but not the names that go with them, maybe I am missing something.</p> </blockquote> <p>You could use <a href="http://play.golang.org/p/FP0XwPQ4Bl"><code>fmt.Printf(&#34;%#v&#34;)</code></a>, but I think that <code>godoc</code> can do what you want. Go 1.5 will introduce <code>go doc</code>, which is more tailored for command-line use. For example, if you want to see all the exported fields and methods <code>json.Encoder</code>, you could just run <code>go doc encoding/json.encoder</code>, and it would list them.</p> <blockquote> <p>How are you supposed to find what file functions are within? I was looking at a few larger projects and it felt daunting to search through all the files in the folder for any given package to find the right file that housed the function that was being called. Is it just based on naming conventions or is there something that points to the specific file (kind of like in python)?</p> </blockquote> <p>This is a bit of an issue which has been complained about before, but the simple syntax in Go makes it pretty easy to find specific things most of the time using something like <code>grep</code>. For example, let&#39;s say you want to find which file in <code>encoding/json</code> defines the <code>Encoder</code> type. You could use <code>grep &#39;type Encoder&#39; *.go</code>.</p> <blockquote> <p>What are the &#34;best&#34; handful of blogs/posts/vids for learning, using, and best practices for go? I will start checking out the &#34;Resources for new Go Programmers&#34; on the sidebar.</p> </blockquote> <p>One that I rather enjoy recommending, if only for the sole reason that Go is pretty much the only language you can do this with, is <a href="http://www.golang.org/ref/spec">the spec</a>. You can literally read through the entire thing in about an hour.</p> <blockquote> <p>Is there a package manager that lets you pull down projects for snap-able pieces (like pip)?</p> </blockquote> <p>I&#39;m not exactly sure how <code>pip</code> works, but you could try <a href="http://getgb.io/"><code>gb</code></a>Go 1.5 will be adding experimental support for vendoring, so hopefully Go 1.6 will have a more full-fledged system available.</p> <blockquote> <p>Is there a multi environment tool that lets you have different versions of go libraries (like virtualenv)?</p> </blockquote> <p>Provided I am understanding your question correctly, this isn&#39;t really necessary, since Go binaries are statically linked. The aforementioned vendoring support should make it possible to determine versions more easily at compile-time as well.</p> <blockquote> <p>Is the convention of returning an error value in (almost) every function and then checking if there was an error in the calling function standard practice? I believe I understand that there is no try/except in go and only to use panic when you really want things to bubble up.</p> </blockquote> <p>Pretty much. A function shouldn&#39;t have an <code>error</code> return if it&#39;s not going to cause an error, though. <code>panic()</code> and <code>recover()</code> usage is pretty rare, so far as I have seen.</p> <p>I hope that helps, and I hope someone can fill in what I missed.</p></pre>golangnewb: <pre><p>Thanks for this!</p> <p>I will take a look at the spec link you provided, and read up on all the new 1.5 changes (exciting to jump in when a new version hits).</p> <p>To find things in the sea of files, I was greping through the repos as you said but was hoping there was a better way.</p> <p>I just tried the fmt.Printf(&#34;%#v&#34;, obj), I think it does the same thing and does not show the names of the fields only what is in the fields if that makes sense. Maybe I am doing something wrong.</p></pre>DeedleFake: <pre><blockquote> <p>I just tried the fmt.Printf(&#34;%#v&#34;, obj), I think it does the same thing and does not show the names of the fields only what is in the fields if that makes sense. Maybe I am doing something wrong.</p> </blockquote> <p>Did you take a look at the link? It does show field names.</p> <p>One thing to keep in mind, though, is that fields in a struct and methods on a type are completely, vastly different things in Go. Fields are what you would expect them to be from Python, but methods are more like functions with some syntactical sugar. They can be defined on any type in the same package that that type is declared, and types do not need to be structs.</p></pre>golangnewb: <pre><p>Didn&#39;t notice it was a link.</p> <p>I see, it works on this demo. I figured out why it did not work for me when I tried to use your line. I used Println not Printf. I will look into the difference!</p> <p>Thanks!</p></pre>Bake_Jailey: <pre><p>I&#39;d also like to recommend <a href="https://github.com/davecgh/go-spew" rel="nofollow">go-spew</a>. I use it a lot for my debugging, as it works recursively. You can make a custom config that does things like format (into a JSON-like look), and let you completely skip the Stringer interface (for pesky types that hide information behind a string).</p></pre>Fwippy: <pre><p>I really recommend using an editor that has some Go support - its not essential (like for C# or Java), but a little help can go a long way. Personally, I use vim and the vim-go plugin, but vim is not really nano. I hear good things about the sublime text go plugin, which is probably closer to what you are used to.</p> <p>For a pip analog, you&#39;ll want &#34;go get&#34; - it downloads the source, any dependencies, and executables (if any).</p> <p>godoc.org is where you can find the best documentation of packages - and if you click the function you&#39;re interested in, it&#39;ll take to right to the definition in the source.</p> <p><a href="https://golang.org/doc/effective_go.html">https://golang.org/doc/effective_go.html</a> and <a href="https://tour.golang.org">https://tour.golang.org</a> are good starting points and could be all the resources you need. Seriously, they&#39;re short, but they&#39;re pretty comprehensive. The go tour is like a tutorial, effective go is closer to your manual.</p> <p>Hope this helps!</p> <p>Edit: Oops, looks like I was beaten to the punch. :)</p></pre>golangnewb: <pre><p>Thanks!</p> <p>I may check out the vim-go plugin. Seems like that is the way people are pointing.</p> <p>Can&#39;t believe I missed &#34;go get&#34;. It&#39;s right there when you type go.</p></pre>izuriel: <pre><p>To your first two:</p> <p>No. What you want doesn&#39;t really exist in Go. I mean, the reflect package let you do all kinds of inspection but you would be silly doing that when there are tools designed to look into Go docs. That said, you want go docs. The site godoc.org is the best resource and is capable of generating documentation for files on public VCS repos. Use it. </p> <p>To your third question: </p> <p>The Go blog is obviously great. In response to a later question they have an article about error handling. Also Effective Go is an article on the go sites that is a go to for idiomatic Go information. </p> <p>The next two: </p> <p>Not really. You&#39;ll find a handful of tools that helps age versions for reproducible builds but they all (basically) wrap the go get command which is your &#34;package&#34; manager. </p> <p>To the last:</p> <p>There is a great article on the go blog about handling errors in more efficient ways that&#39;s great to read. But a more direct answer is yes. Any function that does a &#34;thing&#34; that can fail should return an error. Failure is different than noop situations. Just because you don&#39;t do anything on a given input doesn&#39;t mean you should have errors. I mean situations where something should work but can fail (like bad file pointers, trying to write to a folder or sending data to a database that is no longer available). I also want to make it clear that using panic doesn&#39;t &#34;bubble&#34; an error, it kills the application and prints some error info. Only use it when that&#39;s what you really want which is probably rarely outside of small personal projects. </p> <p>A final note:</p> <p>Get yourself an editor. I can&#39;t believe you would be productive using those editors and definitely wouldn&#39;t let something like that happen on my team. I&#39;m all for letting devs use their tools of choice but I guess the hidden requirement there would be that they have to be dev tools. At the very least use Notepad++ on Windows which at least tries to be a useful editor. </p> <p>Also, sorry there are no links. I&#39;m on a phone and that makes linking a bit more work - Google should find all of this easily though. </p></pre>golangnewb: <pre><p>Thanks for the help!</p> <p>Looks like godoc.org is the place to be!</p> <p>Trust me, I hear it all the time from my team mates about the editors. However the introspection in python and forcing me to learn where things are make me very quick. I used visual studio back when I was a c# dev, intellisense was super sweet, but I feel I know python std libraries way better than I knew c# std libraries.</p> <p>However if things get rough I can always go back to my trusty vim! </p></pre>TheMerovius: <pre><p>One thing I find <em>really</em> important for programming in go (I mean… like… <em>really</em> important, it won&#39;t be any fun otherwise) is automatically running goimports on save. Not only do you want to make <em>sure</em> that all your code is always properly formatted all the time -- missing or superfluous imports are a compilation error in go, so without goimports you will need to adjust your import <em>all the time</em>.</p> <p>I don&#39;t really care what editor you use <em>as long as you can integrate goimports</em>. :) Notepad can&#39;t do that, as far as I know.</p></pre>izuriel: <pre><p>I mentioned Notepad++ because it&#39;s just a text editor. It doesn&#39;t have intellisense it just highlights code and then provides some more code related features.</p> <p>Sublime is a much better choice but is very similar. Sublime offers plugins that make the editor more powerful. Atom is an open source clone (not 100% the same) of sublime. But the key point is they are all just text editors. Not IDEs.</p></pre>

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

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