{"repo":"go-monk/from-bash-to-go-part-ii","free":true,"listed":false,"github":"https://github.com/go-monk/from-bash-to-go-part-ii","clone":"git clone https://github.com/go-monk/from-bash-to-go-part-ii.git","description":"Go for Bash Programmers II - CLI Tools","language":"Go","stars":112,"topics":["from-bash-to-go-series","bash"],"license":null,"category":"cli-tools","readme_excerpt":"This is the second part of a series introducing Bash programmers to Go. This part is about basics of writing CLI tools in Go. See the first part for the language building blocks. Our first CLI tool Bash is often used to write small CLI tools and automation. Let's start with an example CLI tool that prints \"hello\" to terminal. The Bash version is pretty simple: Now, let's implement a Go version. We start by creating a directory where the first version of our program will live. We also initialize a module in there: Since the program is not complex we don't have to think a lot about its design and can easily start with a test: We named the package hello test instead of hello . This is possible and it allows for writing tests that use only the public API (identifiers starting with a capital letter) of the tested package as a real user would. Note that test packages are the sole exception to Go's standard rule that each source directory can contain only one package. In this test we just call the Print function from the hello package. Let's try and run the test: Yes, we have not yet written the code we want to test. So let's do it: If we re-run the test we can see that all is good now. Or is it? Well, something must be wrong because an empty function that does nothing at all (except that it exists) passes the test. So the test is obviously wrong. Now we need to start thinking a bit. What should be actually tested? Making it testable Okay, we want the function to print the string \"h","default_branch":null,"files":null,"tree":[],"storefront":"/r/go-monk","claimed":false,"request_supported":{"post":"https://gitbuyer.com/r/go-monk/from-bash-to-go-part-ii/request-supported","requests":0},"note":"indexed from public GitHub; nothing is for sale on this page. Clone it from GitHub. Paid listings live at /search."}