How to resolve the algorithm Dynamic variable names step by step in the Go programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Dynamic variable names step by step in the Go programming language
Table of Contents
Problem Statement
Create a variable with a user-defined name. The variable name should not be written in the program text, but should be taken from the user dynamically.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Dynamic variable names step by step in the Go programming language
This Go program is a simple interactive command-line application that allows the user to create and inspect integer variables. Here's a detailed explanation of the code:
-
Package and Imports:
- The program starts with the
package main
declaration, which indicates that this is the main package of the program. - It imports necessary standard library packages:
bufio
: For reading input from the standard input stream.fmt
: For formatted printing and input.log
: For logging error messages.os
: For accessing the standard input stream.strconv
: For converting strings to integers.strings
: For string manipulation.
- The program starts with the
-
Error Handling Function:
- The
check
function is defined to handle errors by logging the error message and terminating the program.
- The
-
Main Function:
- The
main
function is the entry point of the program. - It initializes a
scanner
to read input from the standard input stream. - It declares a variable
n
and prompts the user to enter a number between 1 and 5, which will determine the number of integer variables to be created. It keeps prompting until the user enters a valid number.
- The
-
Creating a Map:
- It declares a map
vars
to store the names and values of the integer variables.
- It declares a map
-
Inputting Variable Data:
- It enters a loop to create the specified number of variables.
- For each variable, it prompts the user to enter its name and value.
- It checks for duplicate variable names and prompts the user to enter a new name if a duplicate is found.
- It converts the user's input for value into an integer and stores it in the
vars
map.
-
Interactive Inspection:
- After creating the variables, it enters an infinite loop for interactive inspection.
- It prompts the user to enter the name of a variable they want to inspect.
- It checks if the user has entered 'q' to quit the program.
- If the user has entered a valid variable name, it retrieves its value from the
vars
map and prints it.
-
Additional Features:
- The program uses the
strings.ToLower
function to convert the user's input for the variable name to lowercase, making the inspection case-insensitive. - It uses the
check
function to handle any errors that may occur during input or conversion.
- The program uses the
Source code in the go programming language
package main
import (
"bufio"
"fmt"
"log"
"os"
"strconv"
"strings"
)
func check(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
n := 0
for n < 1 || n > 5 {
fmt.Print("How many integer variables do you want to create (max 5) : ")
scanner.Scan()
n, _ = strconv.Atoi(scanner.Text())
check(scanner.Err())
}
vars := make(map[string]int)
fmt.Println("OK, enter the variable names and their values, below")
for i := 1; i <= n; {
fmt.Println("\n Variable", i)
fmt.Print(" Name : ")
scanner.Scan()
name := scanner.Text()
check(scanner.Err())
if _, ok := vars[name]; ok {
fmt.Println(" Sorry, you've already created a variable of that name, try again")
continue
}
var value int
var err error
for {
fmt.Print(" Value : ")
scanner.Scan()
value, err = strconv.Atoi(scanner.Text())
check(scanner.Err())
if err != nil {
fmt.Println(" Not a valid integer, try again")
} else {
break
}
}
vars[name] = value
i++
}
fmt.Println("\nEnter q to quit")
for {
fmt.Print("\nWhich variable do you want to inspect : ")
scanner.Scan()
name := scanner.Text()
check(scanner.Err())
if s := strings.ToLower(name); s == "q" {
return
}
v, ok := vars[name]
if !ok {
fmt.Println("Sorry there's no variable of that name, try again")
} else {
fmt.Println("It's value is", v)
}
}
}
You may also check:How to resolve the algorithm Mian-Chowla sequence step by step in the Perl programming language
You may also check:How to resolve the algorithm Middle three digits step by step in the BASIC programming language
You may also check:How to resolve the algorithm Sum of squares step by step in the C# programming language
You may also check:How to resolve the algorithm Odd word problem step by step in the Factor programming language
You may also check:How to resolve the algorithm Arrays step by step in the ChucK programming language