TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Diving into Go by building a CLI application

318 pointsby erybalmost 5 years ago

24 comments

boyteralmost 5 years ago
Go is a particularly good language for CLI&#x27;s I have found. At least compared to Java&#x2F;C#&#x2F;Python.<p>It&#x27;s reasonably fast, compiles down to a simple to distribute binary, and the language is forgiving enough that you can do exploratory programming in it. Go-routines make it especially easy to deal with network calls in it as well. For anything that needs absolute performance though look elsewhere, but even then Go might be a good choice for prototyping.<p>I actually started learning Go with CLI applications. I have found that <a href="https:&#x2F;&#x2F;github.com&#x2F;spf13&#x2F;cobra" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;spf13&#x2F;cobra</a> tends to be one of the better CLI helpers you can get into but <a href="https:&#x2F;&#x2F;github.com&#x2F;jpillora&#x2F;opts" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jpillora&#x2F;opts</a> is one I have been meaning to try following a presentation I saw on it once.
评论 #23320218 未加载
评论 #23323387 未加载
评论 #23319770 未加载
评论 #23320411 未加载
评论 #23322674 未加载
评论 #23319684 未加载
评论 #23324033 未加载
评论 #23321369 未加载
评论 #23323836 未加载
dashwavalmost 5 years ago
Since everyone else is throwing out recommendations I personally think <a href="https:&#x2F;&#x2F;github.com&#x2F;spf13&#x2F;cobra" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;spf13&#x2F;cobra</a> is the best CLI templating system, especially because of how well it pairs with <a href="https:&#x2F;&#x2F;github.com&#x2F;spf13&#x2F;viper" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;spf13&#x2F;viper</a>.<p>Large projects like Hugo and Kubernetes have used Cobra to build their CLI tools, and it&#x27;s fairly light as well even if you need simpler usage. We use it at my workplace simply for wrapping our microservices and the few commands (serve, migrate, etc)
评论 #23322684 未加载
评论 #23341670 未加载
评论 #23319974 未加载
评论 #23319464 未加载
sandreasalmost 5 years ago
My Opinion:<p>The best cli lib I found: <a href="https:&#x2F;&#x2F;github.com&#x2F;urfave&#x2F;cli" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;urfave&#x2F;cli</a><p>For deployment I recommend: <a href="https:&#x2F;&#x2F;github.com&#x2F;goreleaser&#x2F;goreleaser" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;goreleaser&#x2F;goreleaser</a><p>During development I recommend: <a href="https:&#x2F;&#x2F;github.com&#x2F;golangci&#x2F;golangci-lint" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;golangci&#x2F;golangci-lint</a> and <a href="https:&#x2F;&#x2F;github.com&#x2F;stretchr&#x2F;testify" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;stretchr&#x2F;testify</a>
评论 #23323103 未加载
评论 #23321932 未加载
chewxyalmost 5 years ago
Everyone&#x27;s throwing out suggestions for CLI libraries, so let me plug my mate&#x27;s: <a href="https:&#x2F;&#x2F;github.com&#x2F;jpillora&#x2F;opts" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jpillora&#x2F;opts</a><p>I definitely prefer it to cobra
pulkitsh1234almost 5 years ago
I wrote a Go CLI Boilerplate sometime back: <a href="https:&#x2F;&#x2F;github.com&#x2F;pulkitsharma07&#x2F;go-cli-boilerplate" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;pulkitsharma07&#x2F;go-cli-boilerplate</a>, it addresses some common issue which I faced while developing a full-fledged CLI.<p>Features (From Readme):<p>* Unit and Integration test structure for the CLI<p>* Opinionated directory structure for organizing code for commands.<p>* Docker-based cross-platform build pipeline<p>* Travis CI-based release workflow<p>* Makefile for common tasks like generating documentation and building the binary.
henvicalmost 5 years ago
I love using Go for CLIs. Previously, I developed CLIs with JavaScript (NodeGH, something with the same goal as GitHub&#x27;s CLI), and &quot;kind of&quot; with PHP as well (for internal tasks on a server). Nothing compares with writing one with Go, though.<p>I used to be the maintainer of a CLI for a PaaS until a year ago: <a href="https:&#x2F;&#x2F;asciinema.org&#x2F;a&#x2F;192043" rel="nofollow">https:&#x2F;&#x2F;asciinema.org&#x2F;a&#x2F;192043</a> <a href="https:&#x2F;&#x2F;github.com&#x2F;henvic&#x2F;wedeploycli" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;henvic&#x2F;wedeploycli</a>
maxioaticalmost 5 years ago
If anyone would like a book on this subject, I recommend <i>Powerful Command-Line Applications in Go</i>: <a href="https:&#x2F;&#x2F;pragprog.com&#x2F;book&#x2F;rggo&#x2F;powerful-command-line-applications-in-go" rel="nofollow">https:&#x2F;&#x2F;pragprog.com&#x2F;book&#x2F;rggo&#x2F;powerful-command-line-applica...</a><p>It&#x27;s currently in Beta but the first 6 chapters are finished and available. As someone learning Go I found it a nice complement to reading <i>The Go Programming Language</i>.
ayoisaiahalmost 5 years ago
I&#x27;ve been building CLIs with Go as part of a 52 projects in 52 weeks challenge[1] and I&#x27;m loving it so far. I released a batch renaming tool [2] just a few days ago.<p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;ayoisaiah&#x2F;project52" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ayoisaiah&#x2F;project52</a> [2]: <a href="https:&#x2F;&#x2F;github.com&#x2F;ayoisaiah&#x2F;goname" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ayoisaiah&#x2F;goname</a>
sascha_slalmost 5 years ago
Whatever you do, do not use the flag library in a package that might ever, EVER be imported. Google did this in the horribly written glog port to go which until recently was used everywhere in Kubernetes. The only way to determine the value of the &quot;v&quot; flag they define globally for your entire executable is to call the V(n) function with n incrementing until it returns false.<p>pflag, which is used by cobra, is a much nicer library.
integriialmost 5 years ago
I wrote my own flags package called flaggy and think its the easiest to use and makes the most sense! Up to 600 stars om github now. <a href="https:&#x2F;&#x2F;github.com&#x2F;integrii&#x2F;flaggy" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;integrii&#x2F;flaggy</a>
gugglealmost 5 years ago
I want something like Argh (<a href="https:&#x2F;&#x2F;github.com&#x2F;neithere&#x2F;argh&#x2F;" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;neithere&#x2F;argh&#x2F;</a>) but for Go... any hint ? There are a gazillion cli libs, it&#x27;s hard to test all of them.
fermienricoalmost 5 years ago
Is it possible to display images in terminal?<p>Terminal is simultaneously powerful and painful tool. I know a guy that refuses to use anything but CLI and suffers a lot. But most basic apps can be written it in like those BIOS menus from a 2005 dell computer.
评论 #23320090 未加载
评论 #23319639 未加载
评论 #23319651 未加载
评论 #23321287 未加载
评论 #23319683 未加载
评论 #23319998 未加载
评论 #23319298 未加载
christiansakaialmost 5 years ago
Wow talk about timing. I just created a simple Markdown viewer with a CSS switcher.<p>Shameless promotion:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;christiansakai&#x2F;md" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;christiansakai&#x2F;md</a>
maalloocalmost 5 years ago
As much as I like Go I don&#x27;t think it&#x27;s ideal for CLI apps.<p>IMO Python is the best at creating simple to complex CLI apps due to it being interpreted and simple. It&#x27;s an overcharged bash.
评论 #23321553 未加载
评论 #23324683 未加载
FlashBlazealmost 5 years ago
Is Go a good option to create some background tasks. For eg. monitor which Spotify songs are playing and and store that in a json file, etc. Or is Python more suitable for this?
评论 #23348648 未加载
mongolalmost 5 years ago
I liked go-flags <a href="https:&#x2F;&#x2F;github.com&#x2F;jessevdk&#x2F;go-flags" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;jessevdk&#x2F;go-flags</a>
zouhairalmost 5 years ago
Just a note, xkcd has 2 image sizes.<p>Small: <a href="https:&#x2F;&#x2F;imgs.xkcd.com&#x2F;comics&#x2F;confidence_interval.png" rel="nofollow">https:&#x2F;&#x2F;imgs.xkcd.com&#x2F;comics&#x2F;confidence_interval.png</a><p>Big: <a href="https:&#x2F;&#x2F;imgs.xkcd.com&#x2F;comics&#x2F;confidence_interval_2x.png" rel="nofollow">https:&#x2F;&#x2F;imgs.xkcd.com&#x2F;comics&#x2F;confidence_interval_2x.png</a><p>Though not for older ones.
评论 #23325460 未加载
radicalriddleralmost 5 years ago
I had a similar idea a couple of months ago. Make a CLI in golang that allows me to easily open all my favorite timewaster sites at the start of the work day. But it ended up in the &quot;Started, but never looked at again&quot; pile. Maybe I&#x27;ll have another look at it on the weekend.
评论 #23318425 未加载
评论 #23318454 未加载
评论 #23319650 未加载
评论 #23319173 未加载
winridalmost 5 years ago
I personally love building CLI tools in Node, since I and my co-workers all have it installed. Nice synchronous STD lib for file manipulation and async&#x2F;await makes for compact async code.<p>If I worked in a Go shop I&#x27;d probably use Go, though.
评论 #23320676 未加载
评论 #23320512 未加载
subsaharancoderalmost 5 years ago
Took your code, added a random number generator and threw it into a Go HTTP server and deployed it as a GCP Cloud Function :-)<p><a href="https:&#x2F;&#x2F;us-central1-bookshelf-app-1103.cloudfunctions.net&#x2F;RandomXkcd" rel="nofollow">https:&#x2F;&#x2F;us-central1-bookshelf-app-1103.cloudfunctions.net&#x2F;Ra...</a><p>Voila!! Serverless Random XKCD..
评论 #23348654 未加载
verdvermalmost 5 years ago
Have a look at <a href="https:&#x2F;&#x2F;github.com&#x2F;hofstadter-io&#x2F;hofmod-cli" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;hofstadter-io&#x2F;hofmod-cli</a><p>It makes framing out advanced Golang CLIs a breeze<p>*(creator)
assafmoalmost 5 years ago
Bash Bonus #2:<p><pre><code> seq 1 10 | xargs -n 1 .&#x2F;go-grab-xkcd -s -n</code></pre>
edemalmost 5 years ago
Why would I dive into Go in the first place?
评论 #23322749 未加载
earthboundkidalmost 5 years ago
Seems overcooked in some places and undercooked in others. I of course prefer my own CLI template but so it goes: <a href="https:&#x2F;&#x2F;github.com&#x2F;carlmjohnson&#x2F;go-cli" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;carlmjohnson&#x2F;go-cli</a>
评论 #23321571 未加载