Fedora - Getting Started with Go on Fedora - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Computers (https://www.sickgaming.net/forum-86.html) +--- Forum: Linux, FreeBSD, and Unix types (https://www.sickgaming.net/forum-88.html) +--- Thread: Fedora - Getting Started with Go on Fedora (/thread-91875.html) |
Fedora - Getting Started with Go on Fedora - xSicKxBot - 09-09-2019 Getting Started with Go on Fedora <div><p>The <a href="https://golang.org/">Go</a> programming language was first publicly announced in 2009, since then the language has become widely adopted. In particular Go has become a reference in the world of cloud infrastructure with big projects like <a href="https://kubernetes.io/">Kubernetes</a>, <a href="https://www.openshift.com/">OpenShift</a> or <a href="https://www.terraform.io/">Terraform</a> for example.</p> <p> <span id="more-29183"></span> </p> <p>Some of the main reasons for Go’s increasing popularity are the performances, the ease to write fast concurrent application, the simplicity of the language and fast compilation time. So let’s see how to get started with Go on Fedora.</p> <h2>Install Go in Fedora</h2> <p>Fedora provides an easy way to install the Go programming language via the official repository.</p> <pre class="wp-block-preformatted">$ sudo dnf install -y golang $ go version go version go1.12.7 linux/amd64</pre> <p>Now that Go is installed, let’s write a simple program, compile it and execute it.</p> <h2>First program in Go</h2> <p>Let’s write the traditional “Hello, World!” program in Go. First create a <em>main.go</em> file and type or copy the following.</p> <pre class="wp-block-preformatted">package main import "fmt" func main() { fmt.Println("Hello, World!") }</pre> <p>Running this program is quite simple.</p> <pre class="wp-block-preformatted">$ go run main.go Hello, World!</pre> <p>This will build a binary from main.go in a temporary directory, execute the binary, then delete the temporary directory. This command is really great to quickly run the program during development and it also highlights the speed of Go compilation.</p> <p>Building an executable of the program is as simple as running it.</p> <pre class="wp-block-preformatted">$ go build main.go $ ./main Hello, World!</pre> <h2>Using Go modules</h2> <p>Go 1.11 and 1.12 introduce preliminary support for modules. Modules are a solution to manage application dependencies. This solution is based on 2 files <em>go.mod</em> and <em>go.sum</em> used to explicitly define the version of the dependencies.</p> <p>To show how to use modules, let’s add a dependency to the hello world program.</p> <p>Before changing the code, the module needs to be initialized.</p> <pre class="wp-block-preformatted">$ go mod init helloworld go: creating new go.mod: module helloworld $ ls go.mod main main.go</pre> <p>Next modify the main.go file as follow.</p> <pre class="wp-block-preformatted">package main import "github.com/fatih/color" func main () { color.Blue("Hello, World!") }</pre> <p>In the modified main.go, instead of using the standard library “<em>fmt</em>” to print the “Hello, World!”. The application uses an external library which makes it easy to print text in color.</p> <p>Let’s run this version of the application.</p> <pre class="wp-block-preformatted">$ go run main.go Hello, World! </pre> <p>Now that the application is depending on the <em>github.com/fatih/color</em> library, it needs to download all the dependencies before compiling it. The list of dependencies is then added to <em>go.mod </em>and the exact version and commit hash of these dependencies is recorded in <em>go.sum</em>.</p></p> </div> https://www.sickgaming.net/blog/2019/08/21/getting-started-with-go-on-fedora/ |