How Do You Troubleshoot Golang Using Hexademical Values?

Problem scenario
Your Golang program has variables with values that look like this:

{0xc4200457e8 [97 49]}

You think the that underlying variables should have regular alphabetic string values. What should you do?

Solution
Use the .string() function.

When you use strings.Builder for a variable, the value will be a hexadecimal value. To retrieve the string value that you intend,

 » Read more..

How Do You Troubleshoot the Golang Error “missing function body”?

Problem scenario
You run a Golang program with a function that uses a map. You get this error:

./foobar.go:33:6: missing function body
./foobar.go:33:44: syntax error: unexpected {, expecting comma or )

What should you do?

Solution
Omit the “{}” in passing map data types to the function. The syntax for declaring a map will include {}.

Here is an example of how to properly define a map:

goodmap := map[string][5]string{}

But when you are defining a function that will accept a map as a parameter,

 » Read more..

How Do You Write a Tic-Tac-Toe Program in Golang?

Problem scenario
You want to play TicTacToe. You have a Golang compiler installed. What should you do?

Solution
Prerequisites
This assumes that you have installed Go. If you need assistance, see this posting.

Procedures
1. Name this program tictactoe.go.

package main

import (
“strings”
“bufio”
“fmt”
“os”
)

func mapper(letter string) int{
if strings.TrimRight(letter,  » Read more..

How Do You Write a Hello World Program in Golang?

Problem scenario
You want to test out the Golang compiler.  You want to know how to write a basic Go program and run it.  What do you do?

Solution
Prerequisites

You must have the Golang compiler installed.  If you need help, see this posting.

Procedures
1.  Create a file called hw.go with the following five lines:

package main
import “fmt”
func main() {
fmt.Println(“This is a Hello World program!”)
}

2. 

 » Read more..

How Do You Install Golang on to Any Type of Linux?

Updated on 10/18/19

Problem scenario
You want to do some coding in the Go programming language.  You want a script to install Golang on any distribution of Linux (e.g., CentOS/Red Hat Enterprise Linux/Fedora, Debian/Ubuntu, or SUSE).  What do you do?

Solution
1.  Create a file in /tmp/ called go.sh with the following content:

version=1.10.8 # Change this version as needed
curl https://storage.googleapis.com/golang/go$version.linux-amd64.tar.gz /tmp/go$version.linux-amd64.tar.gz
cp /tmp/go$version.linux-amd64.tar.gz /usr/local/go$version.linux-amd64.tar.gz
cd /usr/local/
tar xzvf go$version.linux-amd64.tar.gz
#echo “export PATH=””$””PATH:/usr/local/go/bin” ~/.profile
echo “export PATH=””$””PATH:/usr/local/go/bin” /etc/profile.d/go.sh
echo “You will have to log out and log back in for go to work.”
echo “You can use the ‘go version’ command to determine what version was installed”

2. 

 » Read more..

How Do You Install the Go Programming Language on an Ubuntu Linux Server?

Problem scenario
You want to use the Go programming language.  There are various directions online that involve downloading files and modifying configuration files.  You just want simple directions to install Go to an Ubuntu Linux server.  What do you do?

Solution
Run these two commands:
sudo apt-get -y update
sudo apt -y install golang-go

 » Read more..