Book Image

Hands-On Go Programming

By : Tarik Guney
Book Image

Hands-On Go Programming

By: Tarik Guney

Overview of this book

<p>With its C-like speed, simplicity, and power for a growing number of system-level programming domains, Go has become increasingly popular among programmers. Hands-On Go Programming teaches you the Go programming by solving commonly faced problems with the help of recipes. You will start by installing Go binaries and get familiar with the tools used for developing an application. Once you have understood these tasks, you will be able to manipulate strings and use them in built-in function constructs to create a complex value from two floating-point values. You will discover how to perform an arithmetic operation date and time, along with parsing them from string values. In addition to this, you will cover concurrency in Go, performing various web programming tasks, implementing system programming, reading and writing files, and honing many fundamental Go programming skills such as proper error handling and logging, among others. Whether you are an expert programmer or newbie, this book helps you understand how various answers are programmed in the Go language.</p>
Table of Contents (18 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributor
Preface
Index

Processing command-line arguments


In this section, we're going to see how to process command-line arguments. A typical example of a command-line argument is ls -a. Here, a is a command-line argument that is passed to our last command, and ls is a program located in the operating system. Based on the arguments that are passed to the ls command, it behaves differently.

For instance, if we type ls, it will show us all the visible files. If we type ls -a, then it shows everything under that directory, including invisible items as well, which can be seen in the following screenshot:

So, we're going to do the same thing to our program. You can use os.Args to read your arguments that are passed to your application. We are going to read and write these arguments to the console and see how it looks after we passed some arguments to our application. We will have to clear our terminal first and type go run main.go. Since, initially, we are not going to pass any argument, we can expect to see just one...