Golang

 

Introduction

A Frequency Counter is a program that helps us count how many times each character appears in a string. This is a very common task in programming, especially in text analysis, data processing, and natural language processing tasks. The goal of this program is to analyze a given string and output the frequency of each character present in the string.

In this tutorial, we will be using the Go programming language to create a frequency counter. The program will take a string input from the user and return the count of each character that appears in the string.

Objective

The objective of this program is to:

  • Accept a string input from the user.
  • Count the frequency of each character in the string.
  • Display the frequency count of each character in the string.

Go Program Code


package main

import (
    "fmt"
)

func main() {
    // Ask the user for input
    fmt.Print("Enter a string: ")
    var input string
    fmt.Scanln(&input)
    
    // Create a map to store frequency of each character
    freqMap := make(map[rune]int)
    
    // Loop through each character in the string and update its frequency
    for _, char := range input {
        freqMap[char]++
    }
    
    // Display the frequency of each character
    fmt.Println("Character frequencies:")
    for char, count := range freqMap {
        fmt.Printf("%c: %d\n", char, count)
    }
}
            

Explanation of the Program Structure

Let’s break down the code step by step:

  • Importing Packages: We import the fmt package, which provides functions for formatted I/O operations such as printing text to the console.
  • Taking User Input: The fmt.Print function asks the user to input a string. We then use fmt.Scanln to store the input string in the variable input.
  • Creating a Frequency Map: We use a map freqMap where the key is a rune (character), and the value is an integer representing the frequency of that character.
  • Counting Frequencies: The for loop iterates over the string and updates the frequency map. The range keyword helps us iterate through each character (rune) in the string.
  • Displaying Results: Another for loop is used to print the frequency of each character present in the string using fmt.Printf.

How to Run the Program

  1. Install Go programming language from the official website: Go Downloads.
  2. Save the code in a file with a .go extension (for example, frequency_counter.go).
  3. Open the terminal or command prompt and navigate to the folder where the file is saved.
  4. Run the program by typing the following command in the terminal:
    go run frequency_counter.go
  5. After running the program, input a string when prompted, and the program will output the frequency of each character in the string.
© 2025 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)