Go Program to Print Hollow Square with Diagonals Star Pattern

Write a Go program to print the hollow square star pattern with both the diagonals using for loop.

package main

import "fmt"

func main() {

	var i, j, side int

	fmt.Print("Enter Hollow Square With Diagonals Star side = ")
	fmt.Scanln(&side)

	fmt.Println("Hollow Square With Diagonals Star Pattern")

	for i = 1; i <= side; i++ {
		for j = 1; j <= side; j++ {
			if i == 1 || i == side || i == j ||
				j == 1 || j == side || j == side-i+1 {
				fmt.Printf("* ")
			} else {
				fmt.Printf("  ")
			}
		}
		fmt.Println()
	}
}
Go Program to Print Hollow Square with Diagonals Star Pattern

This Go example prints the hollow square with diagonals pattern of a given character.

package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {

	reader := bufio.NewReader(os.Stdin)

	var i, j, side int

	fmt.Print("Enter Hollow Square With Diagonals Star side = ")
	fmt.Scanln(&side)

	fmt.Print("Character to Print in Hollow Square With Diagonals = ")
	ch, _, _ := reader.ReadRune()

	fmt.Println("Hollow Square With Diagonals Star Pattern")

	for i = 1; i <= side; i++ {
		for j = 1; j <= side; j++ {
			if i == 1 || i == side || i == j ||
				j == 1 || j == side || j == side-i+1 {
				fmt.Printf("%c ", ch)
			} else {
				fmt.Printf("  ")
			}
		}
		fmt.Println()
	}
}
Enter Hollow Square With Diagonals Star side = 15
Character to Print in Hollow Square With Diagonals = $
Hollow Square With Diagonals 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.