package main

import (
	"fmt"
)

var paslonPemilu2024 = []string{"Anies-Muhaimin", "Prabowo-Gibran", "Ganjar-Mahfud"}
var parpolPemilu2024 = []string{"PKB", "Gerindra", "PDIP", "Golkar", "Nasdem", "Buruh", "Gelora", "PKS", "PKN", "Hanura", "Garuda", "PAN", "PBB", "Demokrat", "PSI", "Perindo", "PPP", "Ummat"}

func main() {
	for {
		fmt.Println("\n======================================")
		fmt.Println("     PROGRAM PEMILIHAN UMUM 2024     ")
		fmt.Println("--------------------------------------")
		fmt.Println("1. Tampilkan Nama Paslon dan Partai")
		fmt.Println("2. Pilih Paslon dan Partai")
		fmt.Println("======================================")

		var choice int
		fmt.Print("Masukkan pilihan Anda : ")
		fmt.Scanln(&choice)

		if choice == 1 {
			fmt.Println("\n======================================")
			fmt.Println("Pasangan Calon :")
			for i, paslon := range paslonPemilu2024 {
				fmt.Printf("%d. %s\n", i+1, paslon)
			}

			fmt.Println("\nPartai Politik :")
			for i, partai := range parpolPemilu2024 {
				fmt.Printf("%d. %s\n", i+1, partai)
			}
			fmt.Println("======================================")
		} else if choice == 2 {
			var paslonPilihan int
			fmt.Print("\nMasukkan Nomor Paslon yang dipilih : ")
			fmt.Scanln(&paslonPilihan)
			if !(1 <= paslonPilihan && paslonPilihan <= len(paslonPemilu2024)) {
				fmt.Println("Nomor paslon tidak valid.")
				continue
			}

			var partaiKoalisi []string
			switch paslonPemilu2024[paslonPilihan-1] {
			case "Anies-Muhaimin":
				partaiKoalisi = []string{"PKB", "Nasdem", "PKS", "Ummat"}
			case "Ganjar-Mahfud":
				partaiKoalisi = []string{"PDIP", "Hanura", "Perindo", "PPP"}
			case "Prabowo-Gibran":
				partaiKoalisi = []string{"Gerindra", "Golkar", "Buruh", "Gelora", "PKN", "Garuda", "PAN", "PBB", "Demokrat", "PSI"}
			default:
				partaiKoalisi = parpolPemilu2024
			}

			fmt.Println("\n======================================")
			fmt.Printf("Partai Politik Koalisi %s :\n", paslonPemilu2024[paslonPilihan-1])
			for i, partai := range partaiKoalisi {
				fmt.Printf("%d. %s\n", i+1, partai)
			}
			fmt.Println("======================================")

			var partaiPilihan int
			fmt.Print("\nMasukkan Nomor Partai yang dipilih : ")
			fmt.Scanln(&partaiPilihan)
			if !(1 <= partaiPilihan && partaiPilihan <= len(partaiKoalisi)) {
				fmt.Println("Nomor partai tidak valid.")
				continue
			}

			fmt.Println("\n======================================")
			fmt.Println("             PILIHAN ANDA             ")
			fmt.Println("--------------------------------------")
			fmt.Printf("Pasangan Calon : %s\n", paslonPemilu2024[paslonPilihan-1])
			fmt.Printf("Partai Politik : %s\n", partaiKoalisi[partaiPilihan-1])
			fmt.Println("======================================")
		} else {
			fmt.Println("Pilihan tidak valid. Silakan masukkan 1 atau 2.")
			continue
		}

		var exitChoice string
		fmt.Print("\nApakah Anda ingin keluar? (y/n): ")
		fmt.Scanln(&exitChoice)
		if exitChoice == "y" {
			break
		}
	}
	fmt.Println("\n")
} 

Go Online Compiler

Write, Run & Share Go code online using OneCompiler's Go online compiler for free. It's one of the robust, feature-rich online compilers for Go language, running on the latest version 1.10.2. Getting started with the OneCompiler's Go compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as GO and start coding.

Read inputs from stdin

OneCompiler's Go online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample Go program which takes name as input and prints hello message with your name.

package main
import "fmt"

func main() {
  var name string 
  fmt.Scanf("%s", &name) 
	fmt.Printf("Hello %s", name)
}

About Go

Go language is an open-source, statically typed programming language by Google. Go is highly recommended for creation of highly scalable and available web applications.

Some of the products developed using Go are Kubernetes, Docker, Dropbox, Infoblox etc.

Key Features

  • Fast compilation
  • Easy to write concurrent programs
  • Simple and concise syntax
  • Supports static linking
  • Opensource and huge community support.

Syntax help

Data Types

Data typeDescriptionSizeRange
uint88-bit unsigned integer1 byte0 to 255
int88-bit signed integer1 byte-128 to 127
int1616-bit signed integer2 bytes-32768 to 32767
unit1616-bit unsigned integer2 bytes0 to 65,535
int3232-bit signed integer4 bytes-2,147,483,648 to 2,147,483,647
uint3232-bit unsigned integer4 bytes0 to 4,294,967,295
int6464-bit signed integer8 bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
uint6464-bit unsigned integer8 bytes0 to 18,446,744,073,709,551,615
float3232-bit signed floating point number4 bytes±1.5e-45 to ±3.4e38
float64-bit signed floating point number8 bytes±5.0e-324 to ±1.7e308
stringsequence of immutable text
boolStores either true or false1 byteTrue or false

Variables

Variable is a name given to the storage area in order to manipulate them in our programs.

var varible-names datatype;

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition or set of conditions then If or IF-ELSE or Nested If-Elif-Else are used.

If

if(conditional-expression) {
   // code
} 

If-Else

if(conditional-expression) {
   // code
} else {
   // code
}

Nested If-Else

if(conditional-expression) {
   // code
} else if(conditional-expression) {
   // code
} else {
   // code
}

2. For:

For loop is used to iterate a set of statements based on a condition.

for Initialization; Condition; Increment/decrement {  
  // code  
} 

3. Switch:

Switch is an alternative to If-Else-If ladder.

switch conditional-expression {    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
...    
    
default:     
 // code to be executed when all the above cases are not matched;    
} 

Note:

Go doesn't have while or do-while loops like in C.

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

func functionname(parameter-name type) returntype {  
 //code
}