Golang Example- How to print lowercase and upper case of a to z

This post covers two programs.

  • the First program print lowercase a to z using for loop
  • the Second program display uppercase A to Z using for loop.

To understand this example, You should have the following features in the Go language.

Like other programming languages, there is no specific data type for Character representation. We can use rune data type. rune is a primitive data type that contains ASCII code of type integer, and it means rune is an alias for int65 data type in go programming.

Each character in Rune has ASCII code.

How to show Lowercase a to z using for loop golang Example Program

The below program has two ways to display lowercase letters using for loop.

  • First is using ASCII code
  • Second is using character rune type

Inside each iteration of the character, printed character to console using Printf with %c option

package main

import "fmt"

func main() {
 // This is to print a to z using ascii code
 for char := 97; char <= 122; char++ {
  fmt.Printf("%c", char)
 }
 fmt.Println("")

 // Second example to print a to z using character
 for char := 'a'; char <= 'z'; char++ {
  fmt.Printf("%c", char)
 }

}

When the above program is compiled and executed, Output is

abcdefghijklmnopqrstuvwxyz
abcdefghijklmnopqrstuvwxyz

The below program has two ways to display Upper letters using for loop.

  • First is using ASCII code
  • Second is using character rune type

Inside each iteration of the character, Display character to console using Printf with %c option

package main
import "fmt"

func main() {
 // This is to print A to Z using ascii code
 for char := 65; char <= 90; char++ {
  fmt.Printf("%c", char)
 }
 fmt.Println("")

 // Second example to print A to Z using character
 for char := 'A'; char <= 'Z'; char++ {
  fmt.Printf("%c", char)
 }

}

When the above program is compiled and executed, Output is

ABCDEFGHIJKLMNOPQRSTUVWXYZ
ABCDEFGHIJKLMNOPQRSTUVWXYZ

Conclusion

In this tutorial, You learned the go language example program to print lower case and upper case of alphabets from a to z.