How is Golang useful for you personally?

blov · · 626 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I would like to know about Golang usages not as the main language for the project, but rather as a helper language to solve some problems. This can be personal tasks or main project helper tasks. In short - small, secondary tasks.</p> <p>For example, I tried to use language as a batch/terminal alternative - for running more complex commands, working with files and so on. However my impression is that it&#39;s not very well suited for this.</p> <p>I guess small network application is obvious thing, however I don&#39;t find myself often needing some helper tool there.</p> <p>Basically I am looking where could language be used as secondary-helper tool.</p> <hr/>**评论:**<br/><br/>YOU59: <pre><p>One good thing about go is its easy to make standalone binary. I wrote s3 image resizing web service for company today, under 100 lines and compiled and send binary to server and just run it. done!</p> <p>If I have to do that in nodejs, I will need npm install, setup server, might also need web framework like expressjs, then have to repeat some steps again on server.</p></pre>ecmdome: <pre><p>We had a few PHP script run by cron jobs to blast emails. Took me practically no time to convert those to Golang binaries and they run so much faster obviously</p></pre>brokedown: <pre><p>PHP&#39;s lack of concurrency is a great contrast to Go. One of my biggest wins with Go was moving a php application that bused pcntl_fork() to Go.</p></pre>intermernet: <pre><p>I&#39;m currently working on a project that is mostly written in PHP (not my choice) and have found that anything &#34;long running&#34; is a perfect fit for replacing with Go. Usually scheduling services etc.</p> <p>These have so far been &#34;set and forget&#34; in practice. The Go test package and single-binary deployment make for <em>very</em> stable, and <em>very</em> quick deployment of many of these &#34;helper&#34; services.</p> <p>I already have a plan to replace much of the PHP API code with Go, and will hopefully manage to convince everyone that the rest of the PHP cruft has to go &#34;to Go&#34; eventually.</p> <p>Basically, if you&#39;ve got some service in some language like PHP / Python / Ruby or even Bash scripts, then Go can make your life easier. I can only attribute this to the afore mentioned testing package and the ability to deploy a single binary.</p> <blockquote> <p>I tried to use language as a batch/terminal alternative - for running more complex commands, working with files and so on. However my impression is that it&#39;s not very well suited for this</p> </blockquote> <p>I have found the opposite. Complex tasks dealing with files and so on are Go&#39;s forte :-) Sorry if I can&#39;t provide examples, the stuff I&#39;m working on is unfortunately not open-source :-(</p></pre>brokedown: <pre><p>My apps tend to be longer running, with high concurrency. Both client side and server side, these sorts of things are natural strengths for the language.</p> <p>But keep in mind, it&#39;s another tool in a toolbox. If you can do something more efficiently in a shell script, you should do it in a shell script. A large portion of the code I write is prototyping in bash, and most of those prototypes end up being good enough to stick with.</p></pre>devsquid: <pre><p>It sounds like you want Python man. It is amazing for writing scripts.</p></pre>Yojihito: <pre><p>Yeah I wrote a sitemap.xml-parser in Go because I don&#39;t know any Python but instead of 10 lines of code in Python I have now a 4Mb single binary with 45 lines of code.</p> <p>Right tool for the right problem, time to learn some Python 3 I guess.</p></pre>Shammyhealz: <pre><p>While it&#39;s true that Python will pretty much always result in smaller file sizes, deployment in Go is a hundred times easier due to the static binary. If you start using anything outside the Python stdlib, deployment can become extremely painful. Matching the right version of the interpreter, making sure you have all of the dependencies, getting into virtualenv if a dependency you need conflicts with the version required by another script/system package. </p> <p>To be honest, with how cheap storage is nowadays I don&#39;t see why a 4Mb binary is an issue. I think a big part of the reason the binaries are so large is due to embedding the runtime environment. That&#39;s important because it means the minimum binary size is rather large, but they don&#39;t tend to grow too much as the project gets larger. </p></pre>trickos: <pre><p>Why does this 4Mb binary causes problems vs dealing with python deployment issues?</p></pre>Yojihito: <pre><p>I never said it was a problem. But it takes ~10x longer to do such basic things in Go than in Python from my experience.</p> <p>And what deployment issues? I don&#39;t write scripts for other people, only for me.</p></pre>devsquid: <pre><p>Ah yes my site map parser is written in python. I wouldn&#39;t worry about gos file size. It&#39;s statically linked to libraries, so it&#39;s a completely self contained binary. Python would require the python runtime, which is going to be a lot more than 6mbs</p></pre>headhunglow: <pre><p>We have an application at work which does a lot of logging to many different files and I wanted to tail them all. So I used <a href="http://github.com/go-fsnotify/fsnotify" rel="nofollow">gsnotify</a> to write this tool which lets you tail multiple files, or a whole directory. It&#39;s just a little command line tool, but I feel Go was pretty good fit.</p></pre>piva00: <pre><p>I don&#39;t know what your use-case was but GNU tail can tail multiple files, I do that a lot for the same case as yours (an application writes multiple log files in a directory and I want to tail them all sometimes).</p></pre>headhunglow: <pre><p>I don&#39;t think you can tail a directory though (i.e. tail all files in that dir). Our app creates new log files every hour and I often miss the switchover when using GNU tail.</p></pre>antigiven: <pre><blockquote> <p>For example, I tried to use language as a batch/terminal alternative - for running more complex commands, working with files and so on. However my impression is that it&#39;s not very well suited for this.</p> </blockquote> <p>I find it pretty useful for that actually. The os/exec package seems better designed than a lot of languages&#39; equivalent. I also like to use Go when I know I&#39;ll be doing some text processing more complex than can easily be handled with &#34;perl -pe&#34;, or if I need to run a command in the background.</p></pre>hayzeus: <pre><p>For simple jobs, Ruby or Python work well. You are more likely to already have python 2.7 installed. It&#39;s also worth learning to do shell scripts, since that support will definitely be there.</p> <p>On the other hand, once you start needing dependencies go becomes a lot more worthwhile, since you can just compile them in. If you start needing performance, you&#39;ll also want to consider go.</p> <p>I usually start with a shell script. If that gets to be a pain, I&#39;ll move to ruby (since we have that installed -- otherwise I&#39;d use python). If I need things with a lot of dependencies pushed out to many hosts, I give go serious consideration.</p> <p>For a single host, you&#39;re probably better off with python or ruby as you need not worry about provisioning.</p></pre>egonelbre: <pre><ul> <li>Here are few I can share: <a href="https://gist.github.com/egonelbre/ac1eab514607d7a453b1" rel="nofollow">https://gist.github.com/egonelbre/ac1eab514607d7a453b1</a></li> <li>quick line count <a href="https://github.com/loov/qloc" rel="nofollow">https://github.com/loov/qloc</a>.</li> <li>I&#39;ve also used Go as a bootstrap language for projects (i.e. something that needed multiple communicating pieces in Delphi). </li> <li>Used it to convert from html -&gt; dita.</li> <li>had few utilities to diff/compare crash logs between different computers to find offending drivers/programs.</li> <li>I often use Go as one of the prototyping language to clarify the algorithm and understanding of the code. I.e. it has a nice property -- if code looks nice in it, it&#39;s easy to understand.</li> <li>used it as a code-generator for other languages.</li> </ul></pre>sleepydog: <pre><p>I have several little utilities that help me work faster throughout the day.</p> <ul> <li>A tool that uses github code search and assembles a document from comment fragments spread across multiple repositories.</li> <li>A wrapper for the <code>nsupdate</code> utility that can be used to update DNS records on servers that support DDNS, such as Active Directory.</li> <li>A service that scans our IPAM system at work, finds all the switches, and tells me where a server is based on its MAC address(s).</li> <li>A command-line utility for fetching secrets from a number of backends, such as the OS keychain, or a read-only file, or a prompt, or our password vault service, etc.</li> <li>A service that assembles a grafana dashboard of a hypervisor and its guests when you give it either the hypervisor or a single guest name</li> <li>Command-line utility to pull data from Dell&#39;s hardware inventory service (xserv.dell.com) and get warranty information for a server</li> <li>Command-line utility to get information from a Dell server&#39;s iDRAC and format it in JSON.</li> </ul> <p>There are a few more tools that I have written and use personally. Basically when I feel like a shell script just won&#39;t cut it, I reach for Go. That said, I write a <em>lot</em> of shell scripts :)</p></pre>madman2233: <pre><p>I made a web portal that has all my network management web services in frames for convenience. </p> <p>The menu has links to Cacti, intermapper, nagios, unifi, prtg, hostbill, etc and open an iframe. I can change the menu with a webform that writes a csv file. Its great since all my networks have different web services and now i don&#39;t have to worry about bookmarks or remembering the addresses for anything, everything is on a portal for the respective location.</p></pre>Fwippy: <pre><p>Most of my actual projects in Go are bioinformatics-related. There are a lot of situations where we have a perl or python script (or even bash) that performs an important task in our pipeline, but is slow (or terribly sequential). They don&#39;t <em>need</em> to be ported to Go, but every bit I port reduces the demand for our cluster and improves job turnaround time.</p></pre>calebdoxsey: <pre><p>Here&#39;s an example of using go &amp; bash together: <a href="https://github.com/progrium/go-basher" rel="nofollow">https://github.com/progrium/go-basher</a></p></pre>qrv3w: <pre><p>I&#39;ve just used a secondary Go program for fuzzy string matching in a larger Python web server that needs to find matches to user input. I had written in Python before, but in Go it is ~8x faster (which brings the web response time down by eight whole seconds...). Very useful!</p></pre>e-tron: <pre><p>Mostly code in php, Had a requirement earlier to compare two folders (which contains many***** files) (compare all files in those and report the files,folders added/removed/modified) did that in golang... (The best thing i like about golang is that the language is small and simple, which means that we can actually focus on the real problem). Now trying to convince our PM to rewrite our <em>big services</em> in golang</p></pre>burntsushi: <pre><p>My two examples are probably a bit bigger than &#34;secondary helper,&#34; but they are most definitely personal projects that I wrote to solve specific problems.</p> <p>The first, and biggest, is <a href="https://github.com/BurntSushi/wingo" rel="nofollow">Wingo</a>, which is an X11 window manager. Despite the project&#39;s apparent inactivity, I&#39;ve been using it for a few years now continuously. I use it on my personal machine, my work machine and on my media machines hooked up to TVs.</p> <p>The second is <a href="https://github.com/BurntSushi/goim" rel="nofollow">Goim</a>, which is a tool to manage and search a local IMDb database. In particular, I use the fuzzy searching support in Postgres to automatically rename media files. For example, I just bought a copy of the &#34;Legend of Zelda&#34; DVD that I wanted to rip and store on my network. After ripping, I had a bunch of files named like <code>S01E01.mkv</code>. All I had to do was run <code>goim rename -tv &#39;legend of zelda&#39; *.mkv</code> and it fixes them to, e.g., <code>S01E01 - The Ringer.mkv</code>.</p> <p>I loved using Go for both of these projects.</p></pre>nfuhs: <pre><p>If you just want to write shell scripts take a look at Python: <a href="https://www.python.org/" rel="nofollow">https://www.python.org/</a></p> <p>If you want an languge for in app scripting take a look at lua:<a href="http://www.lua.org/start.html" rel="nofollow">http://www.lua.org/start.html</a></p> <p>If you want to stick with golang watch this talk maybe it clears things for you <a href="https://www.youtube.com/watch?v=Q_H4hrUez80" rel="nofollow">https://www.youtube.com/watch?v=Q_H4hrUez80</a></p></pre>notawriterreally: <pre><p>Late to the party, but here&#39;s my 2 cents:</p> <p>Utility scripts. I also use go for several &#34;full&#34; services but thats not your question.</p> <p>I previously used python/bash(also on windows using git-bash) for all scripts related to deployment, building resource bundles, etc used for my cross-platform c++ application. But imo it is just a lot simpler and cleaner to use go for that. There are multiple reasons. Perhaps the most important is the &#34;everything is unicode in go&#34; vs &#34;dealing with unicode is hard/annoying/sucks in python/bash&#34;. If you are dealing with unicode then go &gt; python/bash every time. Also the ease of http(io)+json/xml. This admittedly is also easy in python but you typically end up with external dependencies in a virtualenv or something, while with go all you need is the runtime installed since the standard library has everything you need for that. And if you don&#39;t want to install the runtime you can save an (admittedly large) static binary in your repo per os and just run the binary after checking out the project.</p> <p>I guess i&#39;m biased since i use the concurrency features of go quite a lot in other projects and have become comfortable with this simple but powerful language. Thus i probably ignore the overhead of using go over python/bash for simple scripts etc. </p></pre>drvd: <pre><p>Pythonlang and Bashlang mit be worth considering.</p></pre>010a: <pre><p>Its great for writing high-performance services that don&#39;t do a lot of network communication. Also, large command line apps.</p> <p>I think it is highly <em>highly</em> overrated for writing web apps. When dealing with JSON you have the option of either (A) throwing away type safety and sacrificing some performance, or (B) creating <a href="https://github.com/ChimeraCoder/gojson" rel="nofollow">massive struct definitions</a> for every JSON message you think you might receive. This becomes almost unbearable when you start dealing with websockets, as many websocket APIs just have a &#34;type&#34; field then the structure of the rest of the message changes based on what type it is. </p> <p>Also, the performance gain over, say, Node is surprisingly small once you do an apples-to-apples comparison with Node Cluster. In fact, the type of workload of most web apps lends itself to being faster on Node cluster. Most of Go&#39;s lauded &#34;hyper fast performance&#34; is in comparison to Ruby or Python. Against Java or Node, they&#39;re all in the same magnitude. Something I think many people forget. </p> <p>People laud how easy it is to deploy apps, but honestly I&#39;m not seeing it. Do people actually cross-compile the app on their dev machine then upload the binary to a cloud server? Deploying is just as easy as (most) other languages. Python has venv. Node has self-contained dependencies in npm. Go has static binaries. Sure, its hella easy compared to the abomination that is Java dependencies, but no easier than any other &#34;modern&#34; language.</p> <p>And for all the (deserved) love we give it for generating static binaries, its method of handling dependencies feels like something straight out of 1992. Its 2015. We have git tags. </p> <p>I digress. I think Go is one of my favourite programming languages. But I swear that people treat it like its the Jesus Language. In reality it is very good at <em>exactly</em> what it was designed for at Google, but people were too quick to adopt it for <em>everything</em>. </p></pre>calebdoxsey: <pre><blockquote> <p>This becomes almost unbearable when you start dealing with websockets, as many websocket APIs just have a &#34;type&#34; field then the structure of the rest of the message changes based on what type it is.</p> </blockquote> <p>There are many 3rd party packages which allow you to work with JSON in other ways. In this case though you can just use the standard package: (<a href="https://play.golang.org/p/cXa3UXBsm3">https://play.golang.org/p/cXa3UXBsm3</a>)</p> <pre><code>package main import ( &#34;encoding/json&#34; &#34;fmt&#34; &#34;strings&#34; ) type Message struct { Type string Data []byte } func (m *Message) UnmarshalJSON(data []byte) error { var tmp struct{ Type string } json.Unmarshal(data, &amp;tmp) m.Type = tmp.Type m.Data = data return nil } func main() { raw := `{ &#34;type&#34;: &#34;whatever&#34;, &#34;arbitrary&#34;: [1,2,3,4,5] }` var msg Message json.NewDecoder(strings.NewReader(raw)).Decode(&amp;msg) fmt.Println(msg) } </code></pre></pre>010a: <pre><p>There is no issue surrounding the fact that you <em>can</em>. The point is that the type system Go&#39;s designers have chosen, in this very particular use case, becomes either useless or cumbersome depending on the way you implement it. In your example; cumbersome. </p> <p>I don&#39;t want Go&#39;s type system to change, though. Its simplicity makes understanding new code much simpler than Java or even Python. It gives you enough structure to not shoot yourself in the foot, but not so much that your entire app <em>becomes</em> structure. </p> <p>A language does not need to be all things to all people. What go does, it does well. What it does not do well is this. </p></pre>calebdoxsey: <pre><p>I don&#39;t understand your argument. You think 10 lines of code and &lt; 5 minutes of work is cumbersome?</p> <p>If you don&#39;t like strong typing you can use a different json library. For example: <a href="http://godoc.org/github.com/stretchr/objx" rel="nofollow">http://godoc.org/github.com/stretchr/objx</a></p> <pre><code>// use MustFromJSON to make an objx.Map from some JSON m := objx.MustFromJSON(`{&#34;name&#34;: &#34;Mat&#34;, &#34;age&#34;: 30}`) // get the details name := m.Get(&#34;name&#34;).Str() age := m.Get(&#34;age&#34;).Int() // get their nickname (or use their name if they // don&#39;t have one) nickname := m.Get(&#34;nickname&#34;).Str(name) </code></pre> <p>There&#39;s nothing fundamental here having to do with the language.</p></pre>010a: <pre><blockquote> <p>I don&#39;t understand</p> </blockquote> <p>Clearly. </p></pre>

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

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