How Do You Pass Parameters to a Function in Golang and Assign the Returned Value to a Variable?

Problem scenario
You want to pass parameters to a function and assign the returned value to a parameter using the Go programming language. What should you do?

Solution
Prerequisite
This assumes that you have installed Golang; if you need assistance with this, see this posting.

Procedures
1.

 » Read more..

How Do You Troubleshoot the Go Programming Error ‘non-name m1[“foo”] on left side of :=’?

Problem scenario
You are running a Go program. You see this message about “command-line-arguments”:

non-name m1[“foo”] on left side of :=

What should you do?

Possible Solution
Can you try to use “=” instead of “:=”? This may fix your problem.

The := is a variable declaration and an assignment. If your map has been declared,

 » Read more..

How Do You Use a Function in Golang?

Solution
Prerequisite
This assumes that you have installed Golang; if you need assistance with this, see this posting.

Procedures
1. Create a file called b.go with this as the content:

package main

import “fmt”

// ContintFunction will display a message
func ContintFunction() {
fmt.Println(“Hello from Continual Integration”)
}

func main() {
ContintFunction()
}

2.

 » Read more..

How Do You Create Bytecode with Golang?

Problem scenario
You want to create bytecode using Golang. What do you do.

Solution
Prerequisite
Install Golang. If you need assistance, see this posting.

Procedures
Create a Go script that you can run with go run nameOfProgram.go
Run this: go build nameOfProgram.go
Run this: ./nameOfProgram

 » Read more..

How Do You Create a List of Strings in Golang?

Problem scenario
Lists have a fixed length number of discrete items. You want the items in the list to be strings. How do you create a list of strings in Golang?

Solution
1. Use this program called animal.go:

package main

import “fmt”

func main() {
var x [10]string
x[0] = “dog”
x[1] = “cat”
x[2] = “hamster”
x[3] = “rabbit”
x[4] = “chicken”

for i := 0;  » Read more..

How Do You Read in User Input with Golang?

Problem scenario
You want to read in user input with Golang. What should you do?

Solution
This program will illustrate how you accept user input with a Golang program:

package main

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

func main() {
fmt.Println(“Please enter some text:”)
reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString(‘\n’)
fmt.Println(“The text you entered was this: “, text)
}  » Read more..

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..