# Golang Program to Print Half Diamond Star Pattern

In this tutorial, we will learn how to print half diamond star pattern using Go programming language.

### Syntax

for initialization; condition; update {
statement(s)
}


To use a for loop in go lang we first need to initialize a variable then specify the condition of the loop i.e when should the loop end then we need to increment or decrement the variable followed by the lines of codes that should get executed.

## Example 1: Golang Program Code to print Half Diamond Star Pattern Using a Single Function

### Approach

The goal is to divide the pattern into an upper and a bottom half. Then, using loops, print each pattern independently. The main finding for printing the upper and lower halves is as follows −

### Algorithm

• Step 1 − Import the package fmt.

• Step 2 − Start function main().

• Step 3 − Declare and initialize the variables.

• Step 4 − Use the for loop with to print the half diamond star pattern.

• Step 5 − Print the result using fmt.Println().

### Example

// GOLANG PROGRAM TO PRINT DIAMOND STAR PATTERN
package main

// fmt package provides the function to print anything
import "fmt"

// start the function main ()
// this function is the entry point of the executable program
func main() {

// Declare the integer variables
var i, j int

// initialize the rows variable
column := 8
count := 1

// initialize the rows variable
fmt.Println("GOLANG PROGRAM TO PRINT HALF DIAMOND STAR PATTERN")
//THIS LOOP IS TO PRINT THE NUMBER OF STAR FOR COLUMNS
i = 1
for i < column * 2 {
j = 1
for j <= count {
fmt.Printf("*")
j++
}
// CONDITIONAL STATEMENT FOR PRINTING * FOR HALF DIAMOND
if i < column {
count++
} else {
count--
}
// TO BREAK THE LINE
fmt.Printf("\n")
i++
}
}


### Output

GOLANG PROGRAM TO PRINT HALF DIAMOND STAR PATTERN
*
**
***
****
*****
******
*******
********
*******
******
*****
****
***
**
*


### Description of the Code

• In the above program, we first declare the package main

• We imported the fmt package that includes the files of package fmt

• Now start the function main() and this function is the entry point of the executable program. It does not take any argument nor return anything

• Declare the four integer variables i, j, N and column and assign appropriate values to them. Initialize the N variable to an integer value you want for the number of rows of the diamond star pattern. Here, fmt.Scanln () function is used to read and store the rows value.

• Using for loop to print the required pattern. The condition is given inside an if statement and increase the “count” if the condition is right.

• And finally printing the result in the form of a Half diamond shape on the screen using fmt.Println() function which formats using the default formats for its operands and writes to standard output.

## Example 2: Golang Program code to Print Half Diamond Star Pattern in two Separate Functions

### Algorithm

• Step 1 − Import the package fmt.

• Step 2 − Create the function halfdiamond().

• Step 3 − Declare and initialize the variables.

• Step 4 − Use of for loop with condition and incrementor.

• Step 5 − Start the function main().

• Step 6 − Calling the function halfdiamond().

• Step 7 − Print the result using fmt.Println().

### Example

// GOLANG PROGRAM TO PRINT DIAMOND STAR PATTERN
package main

// fmt package provides the function to print anything
import "fmt"

// create a function for the diamond star pattern
func halfdiamond(column int) {

// initialize the counter
count := 1

// THIS LOOP IS TO PRINT THE NUMBER OF STAR FOR COLUMNS
i := 1
for i < column*2 {
j := 1
for j <= count {
// printing the star character
fmt.Printf("*")
j++
}
// CONDITIONAL STATEMENT FOR PRINTING * FOR HALF DIAMOND
if i < column {
count++
} else {
count--
}
// TO BREAK THE LINE
fmt.Printf("\n")
i++
}
}
// start function main ()
// this function is the entry point of the executable program
func main() {

fmt.Println("GOLANG PROGRAM TO PRINT DIAMOND STAR PATTERN\n")
// calling the function halfdiamond() and passing the number of rows to be printed as an argument
// to the function.
halfdiamond(8)
}


### Output

GOLANG PROGRAM TO PRINT HALF DIAMOND STAR PATTERN
*
**
***
****
*****
******
*******
********
*******
******
*****
****
***
**
*


### Description of the Code

• In the above program, we first declare the package main

• We imported the fmt package that includes the files of package fmt.

• Now create a function halfdiamond() that contains the logic to print only the half pattern of a diamond star pattern

• Declare the four integer variables i, j, count and column and assign appropriate values to them. Initialize the column variable with an integer value. Here, fmt.Scanln() function is used to read and store the rows value.

• Now the for loop is used, in order to print the required pattern. A condition is given inside an if statement which stops execution if mentioned condition is right.

• Next, start the function main() and this function is the entry point of the executable program. It does not take any argument nor return anything.

• Under main(), the function halfdiamond () function is called for the pattern and the number of rows that star pattern should carry is passed as an argument to it.

• Finally, the result in the form of a diamond star is printed on the screen using fmt.Println () function which formats using the default formats for its operands and writes to standard output.

## Conclusion

We have successfully compiled and executed the Golang program code to printHalf diamond star pattern in the above two examples.

We have used two approaches here one is by using the main() function and also by calling the different function.

Updated on: 15-Nov-2022

233 Views 