Go Program to Print Hollow Rectangle Star Pattern

Write a Go Program to Print Hollow Rectangle Star Pattern. In this Golang Hollow Rectangle Star pattern example, the nested for loop iterates rectangle rows and columns. The if statement (if i == 0 || i == row-1 || j == 0 || j == col-1) checks if it is a first row or last row or first column or last column. If true, print *; otherwise, print empty space.

package main

import "fmt"

func main() {

    var i, j, row, col int

    fmt.Print("Enter the Hollow Rectangle Rows = ")
    fmt.Scanln(&row)

    fmt.Print("Enter the Hollow Rectangle Columns = ")
    fmt.Scanln(&col)

    fmt.Println("Hollow Rectangle Star Pattern")
    for i = 0; i < row; i++ {
        for j = 0; j < col; j++ {
            if i == 0 || i == row-1 || j == 0 || j == col-1 {
                fmt.Print("* ")
            } else {
                fmt.Print("  ")
            }
        }
        fmt.Println()
    }
}
Go Program to Print Hollow Rectangle Star Pattern 1

This Golang program allows entering the symbol and prints that symbol in a hollow rectangle pattern.

package main

import "fmt"

func main() {

    var i, j, row, col int
    var sym string

    fmt.Print("Enter the Hollow Rectangle Rows = ")
    fmt.Scanln(&row)

    fmt.Print("Enter the Hollow Rectangle Columns = ")
    fmt.Scanln(&col)

    fmt.Print("Symbol to Print Hollow Rectangle = ")
    fmt.Scanln(&sym)

    fmt.Println("Hollow Rectangle Star Pattern")
    for i = 0; i < row; i++ {
        for j = 0; j < col; j++ {
            if i == 0 || i == row-1 || j == 0 || j == col-1 {
                fmt.Printf("%s ", sym)
            } else {
                fmt.Print("  ")
            }
        }
        fmt.Println()
    }
}
Enter the Hollow Rectangle Rows = 10
Enter the Hollow Rectangle Columns = 20
Symbol to Print Hollow Rectangle = $
Hollow Rectangle Star Pattern
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 
$                                     $ 
$                                     $ 
$                                     $ 
$                                     $ 
$                                     $ 
$                                     $ 
$                                     $ 
$                                     $ 
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.