Golang

 

Generate a list of prime numbers up to a given limit using the Go programming language.

Introduction

Prime numbers are numbers greater than 1 that have no divisors other than 1 and themselves. In this article, we will learn how to write a simple program in Go that generates prime numbers up to a given limit. This program will help you understand basic loops, conditionals, and how to work with numbers in Go.

Objective

The goal of this program is to provide a list of prime numbers up to a specified limit. The user will input the limit, and the program will display all prime numbers between 1 and the given limit.

Prime Number Generator Code in Go

package main

import "fmt"

// Function to check if a number is prime
func isPrime(n int) bool {
    if n <= 1 {
        return false
    }
    for i := 2; i*i <= n; i++ {
        if n%i == 0 {
            return false
        }
    }
    return true
}

// Function to generate primes up to a given limit
func generatePrimes(limit int) []int {
    primes := []int{}
    for i := 2; i <= limit; i++ {
        if isPrime(i) {
            primes = append(primes, i)
        }
    }
    return primes
}

func main() {
    var limit int
    fmt.Print("Enter the limit: ")
    fmt.Scan(&limit)
    
    primes := generatePrimes(limit)
    fmt.Println("Prime numbers up to", limit, "are:", primes)
}

Explanation of the Program Structure

The program is divided into several parts:

  • isPrime function: This function checks if a given number is prime. It does this by checking if the number is divisible by any number from 2 up to the square root of the number. If it is divisible, it’s not a prime.
  • generatePrimes function: This function takes the limit as input and generates a list of prime numbers by calling the isPrime function for each number from 2 up to the limit.
  • main function: In the main function, the user is prompted to enter the limit, and the program generates the prime numbers up to that limit using the generatePrimes function. Finally, the program prints the list of prime numbers.

How to Run the Program

Follow these steps to run the program:

  1. Install Go programming language from the official website (https://golang.org/dl/).
  2. Create a new file with a .go extension (e.g., prime_number_generator.go).
  3. Copy and paste the code provided above into the file.
  4. Open your terminal or command prompt and navigate to the directory where the file is saved.
  5. Run the command: go run prime_number_generator.go
  6. Enter the limit when prompted, and the program will display all prime numbers up to that limit.
© 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 :)