Go Programming Language For Dummies
Book image
Explore Book Buy On Amazon
Go is a popular open-source programming language (designed at Google in 2007) used for a variety of different applications, including cloud-based or server-side applications, DevOps and site reliability automation, command-line tools, artificial intelligence, and data science.

You can try out Go (sometimes referred to as Golang) programming language online — you just need to know where to go. You also may want to know how to convert JSON to Go or use Go in Docker. This Cheat Sheet tells you how to do all of these things.

Go programming language Source: © Eny Setiyowati / Shutterstock.com

Try Go programming language online

You may not want to install Go on your local computer. Maybe you’re using a locked-down company-issued computer, or you don’t have a computer of your own. Whatever the reason, you can still try Go programming using a number of online tools and integrated development environments (IDEs). Here are three of them that are pretty cool:

  • The Go Playground: The Go Playground is a web service running on the servers of https://golang.org, the official website of Go. Using the Go Playground, your Go application runs inside a sandbox, and you can try Go programming with most of the standard library.

One of the most commonly used features is code sharing. Clicking the Share button enables you to share your code via a fixed URL. This feature is very useful for developers who want to exchange code snippets or for instructors who want to provide students with sample code. Students can directly make changes to the code, compile it, and see the output.

  • JDoodle Online Go Lang IDE: Another online Go IDE is JDoodle Online Go Lang IDE. Although the Go Playground is straightforward and easy to use, the only way your program can interact with the outside world is through the output console. If your program needs user input (such as using fmt.Scanln()), you’re out of luck. This is where the JDoodle Online Go Lang IDE comes in handy. It allows you to create an interactive Go program that accepts inputs as well as generates outputs. Plus, you can save your programs to the cloud or save a local copy on your computer. You can also upload a local file to the JDoodle Online Go Lang IDE.

Perhaps the most interesting features of the JDoodle Online Go Lang IDE is its collaborative/peer programming feature, which allows more than one person to simultaneously modify a program, making it a very effective tool for online training and learning.

  • Replit Go Online Compiler & Interpreter: The Go Online Compiler & Interpreter from Repl.it allows users to upload files and folders. It also has an interactive console where your Go program can accept inputs and display outputs.

One useful feature of the Repl.it IDE is its integration with GitHub for version control. It also supports interactive code sharing for peer programming.

Converting JSON to Go

One of the data formats you’ll encounter very often in your programming career is JavaScript Object Notation (JSON). When you receive your data in the JSON format, you need to convert it into a format that’s easy to manipulate in your Go program. Here are some online tools that allow you to convert your JSON strings straight into Go structs:

Using Golang in Docker

If you’re a Docker user, you can run your Go programs from within a Docker container.

To install a sample Go program in a Golang Docker container, type the following command in Terminal/Command Prompt:

$ docker run golang go get -v

github.com/golang/example/hello/...

This command installs a sample Go program into a Golang container. The ellipses (…) represents instructions to download, build, and install all the things in that repository (including libraries and binaries).

You now have a Golang container containing the sample Go program. Now you need to commit the container to a new Docker image so that, in the future, when you want to run this program, you can just create a container from this new image. Type the following command in Terminal/Power Shell (for Windows users):

$ docker commit $(docker ps -lq) hellogo

The docker ps -lq command returns the ID of the last container executed. This command commits the container that has the sample Go program to a new Docker image called hellogo. You can verify this with the following command:

$ docker images

REPOSITORY  TAG     IMAGE ID      CREATED             SIZE

hellogo     latest  2c1d28ef93cb  About a minute ago  723MB

Now run the sample Go program from the newly created hellogo Docker image:

$ docker run hellogo hello

Hello, Go examples!

If you see this output, your Go program has executed correctly.

Sometimes you just want to run your Go program in a “throwaway” container (just to ensure that your program runs, for example). To do that, you can use the following command:

$ docker run --rm golang sh -c "go get

 github.com/golang/example/hello/... && exec hello"

Hello, Go examples!

The preceding command runs a Golang container, fetches the sample Go program, and uses the shell (sh) to execute (exec) the hello program.

You may want to run your Go program using a specific version of Go. The following command runs the sample Go program using Go version 1.5 in a throwaway container:

$ docker run --rm golang:1.5 sh -c "go get

 github.com/golang/example/hello/... && exec hello"

Unable to find image 'golang:1.5' locally

1.5: Pulling from library/golang

357ea8c3d80b: Pull complete

...

...

Status: Downloaded newer image for golang:1.5

Hello, Go examples!

About This Article

This article is from the book:

About the book author:

Wei-Meng Lee is founder of Developer Learning Solutions, specializing in hands-on technology training. His name regularly appears in publications like DevX.com, MobiForge.com, and CODE Magazine. He is also the author of SwiftUI For Dummies, Beginning Swift Programming, Python Machine Learning, and Learning WatchKit Programming.

This article can be found in the category: