Go Program to Convert a String to Uppercase

1. Introduction

Strings play a pivotal role in various applications, from web development to data analytics. A frequent operation on strings is converting them to uppercase. Such an operation is handy for text normalization in scenarios like creating constants, formatting outputs, etc. While Go does have built-in functionality to turn strings to uppercase, constructing it from scratch is an enriching experience. This post dives into the creation of a Go program that transmutes a string to its uppercase version without leaning on the built-in solution.

2. Program Overview

Our crafted Go program will:

1. Takes a string from the user.

2. Manually convert the given string into its uppercase avatar.

3. Display the altered string.

3. Code Program

// Our journey begins with the declaration of the main package.
package main

// The fmt package to handle input and output.
import "fmt"

// Our custom function to turn a string to uppercase.
func toUpperCase(str string) string {
    result := []rune(str) // We shape the string into a slice of runes for granular character operations.

    // Each rune is then perused.
    for i, r := range result {
        if r >= 'a' && r <= 'z' {
            result[i] = r - 32 // The ASCII magic! Convert lowercase characters to uppercase.
        }
    }
    return string(result) // The transformed rune slice takes on its string form once more.
}

// The central function, our main function.
func main() {
    var inputString string

    // The user's string is requested.
    fmt.Print("Enter a string: ")
    fmt.Scanln(&inputString)

    // The transmuted string is presented.
    fmt.Printf("Uppercase version: %s\n", toUpperCase(inputString))
}

Output:

Assuming the user introduces the string hello, the program will exclaim:
Uppercase version: HELLO

4. Step By Step Explanation

The program defines a custom function named toUpperCase that takes a string as an argument and returns its uppercase version. Inside this function: 

1. Package and Import: It begins by declaring the main package and imports the fmt package for input and output functionalities. 

2. Uppercase Conversion Function: The program defines a custom function named toUpperCase that takes a string as an argument and returns its uppercase version.

  • The input string is converted into a slice of runes, which allows for more fine-grained character manipulations. 
  • A loop iterates through each rune (character) of the string. If a rune corresponds to a lowercase English letter (checked using its ASCII value), it gets converted to its uppercase counterpart using a simple ASCII conversion logic (subtracting 32).
  • Finally, the modified slice of runes is converted back to a string and returned. 

3. Main Function: The main function serves as the entry point of the program: It declares a variable inputString to store user input. The program prompts the user to input a string. This input string is then passed to the toUpperCase function, and the returned uppercase string is displayed to the user.

Comments