👨‍💻
Dokumentasi Golang (PT Phincon)
  • 🚀Dokumentasi Bootcamp (PT Phincon)
  • Command di Golang
  • Static Type di Golang
  • Variable and Constant
    • Variable daI Constant
    • Deklarasi Variabel
    • Iota (Auto Increment)
    • Blank Identifier (Variable Underscore)
    • Access Modifier (Public or Private)
  • Tipe Data Primitif
    • Integer dan Unsigned Integer
    • Float dan Complex
    • String
    • Boolean
  • Tipe Data Aggregate
    • Array
    • Struct
  • Tipe Data Reference
    • Slice
    • Map
    • Function in Golang
      • Function
      • Function vs Method
      • Function vs Procedure
      • Private vs Public Function
      • Function Init dan Main
      • Function dengan Return Multiple Value
      • Variadic Function
      • Function sebagai Parameter
      • Anonymous Function
      • Input Function dari CLI
      • Exercise (Function)
  • Default Value setiap Tipe Data
  • Type Declaration di Golang
    • Type Declaration
    • Type Declaration Built-In di Golang
  • Kebab Case, Camel Case, Snake Case & Pascal Case di Golang
  • Konversi Tipe Data
  • If-Else dan Switch
    • If Else
    • Switch
    • Exercise
  • Looping
    • Looping For
    • Looping for range
    • Infinite Looping
    • Penerapan Looping pada Sorting
  • Operator di Golang
    • Arithmetic Operator
    • Assignment Operator
    • Relational Operator
    • Logic Operator
  • Interface
  • Interface Kosong atau Any
  • Nil
  • Pointer
    • Pass By Value dan Pass By Reference
    • Pointer (Pass By Reference)
    • Operator Address (& dan *)
    • Default Value Pointer
    • Tricky Case
    • Pointer pada Parameter di Function
    • Pointer pada Method
  • Package
    • Fmt
    • Rand
    • Os
    • Strings
      • To Lower
      • Contains
      • Split
      • Trim
      • Atoi
      • Itoa
      • EqualFold
    • Random Generator
    • Time
      • Get Current Time By Location
      • Time Sleep
      • Time Since
      • Timer & After
      • AfterFunc
      • Ticker & Tick
  • Go dan JSON
    • JSON vs XML
    • Unmarshal vs Marshal
    • Marshal (Go Object -> JSON)
    • Unmarshal (JSON -> Go Object)
    • Streaming Decoder & Encoder
    • Tag
    • JSON Go Return Byte
  • Go dan CSV
    • Insert Data ke File CSV
    • Insert 1.000.000 Data ke File CSV
  • Goroutine
    • Concurrency vs Parrarel
    • Go Routine Sederhana
    • Go Routine vs Synchronous
    • Wait Group
    • Defer
    • Channel
    • Buffered Channel
    • Select Channel
    • Deadlock - All goroutines are asleep
    • Race Condition
    • Mutex (Mutual Exclusion)
    • RW Mutex vs Mutex
    • Once
    • Pool
    • Atomic
    • Go Max Procs
    • Exit
    • Exercise 1 : Go Routine + Context + Channel
    • Exercise 2 : Worker (Go Routine + Channel + Context)
    • Exercise 3 : Random Worker (Go Routine + Channel + Context)
    • Exercise : Implementasi Goroutine dan Channel pada File CSV
  • Go Context
    • Pengenalan Context
    • Context Background & TODO
    • Context With Value
    • Context WithDeadline dan Context WithTimeout
    • Context WithCancel dan Context Done
  • Pengenalan HTTP
  • Go Native HTTP
    • HTTP Server
    • HTTP Server Multi Handler
    • HTTP Server dengan Serve Mux
    • HTTP Response Writer
    • HTTP Test
    • Routing
    • Konsep Middleware
    • Middleware
    • Get Query Parameter
    • Get Path Parameter
    • Request JSON
    • Request Form
    • Get dan Set Header
    • Get dan Set Cookie
    • Redirect
    • Serve File
    • Upload File
    • Download File
    • Hit Endpoint dengan Curl di Terminal
  • Go Gin Framework
    • HTTP Server
    • Router
    • Middleware
    • Get Query Parameter
    • Get Path Parameter
    • Request JSON
    • Request Form
    • Get dan Set Header
    • Get dan Set Cookie
    • Redirect
    • Serve File
    • Upload File
    • Download File
  • Golang dan Database
    • Instalasi MySQL dengan Docker Desktop
    • Instalasi PostgreSQL dengan Docker
    • Basic SQL
    • SQL Join
    • SQL Relation
    • Golang Database Driver
    • Golang dan SQL
  • Go Unit Test
    • Method di Package Testing
    • Package Assert & Require
    • Running Sub Test
    • Table Test
    • Generate Coverage Unit Testing dalam Bentuk HTML
  • Sonar Qube dan Sonar Scanner
  • Logging
  • Golang dan Redis
  • Golang dan RabbitMQ
    • Instalasi RabbitMQ dengan Docker
    • Instalasi Package RabbitMQ di Golang
    • Publisher dan Consumer
    • Publisher dan Multi Consumer
    • Setting RabbitMQ (Durable, Auto Delete dan Ack)
  • Git Command
  • Git Clone dengan SSH
  • Anotasi dan Package di Java Spring Boot
Powered by GitBook
On this page
  1. If-Else dan Switch

Exercise

  1. Buatlah program menggunakan keyword if else untuk mengevaluasi variable score dan menampilkan output dengan variabel result dengan ketentuan:

  • Jika nilai score lebih atau sama dengan 90. Isi variabel result dengan nilai: 'Selamat! Anda mendapatkan nilai A'.

  • Jika nilai score ada di antara 80 hingga 89. Isi variabel result dengan nilai: 'Anda mendapatkan nilai B.'

  • Jika nilai score ada di antara 70 hingga 79. Isi variabel result dengan nilai: 'Anda mendapatkan nilai C.'

  • Jika nilai score ada di antara 60 hingga 69. Isi variabel result dengan nilai: 'Anda mendapatkan nilai D.'

  • Jika nilai score di bawah 60. Isi variabel result dengan nilai: 'Anda mendapatkan nilai E.'

  1. Buatlah program terdapat price, qty dan total, total didapat dari perkalian price dan qty dimana jika terdapat discount maka total berkurang 10%

  2. Buatlah program menghitung jumlah string huruf vocal pada kata “Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.”

  3. Buatlah program yang menghitung factorial 30

  4. Buatlah program dimana mencari bilangan itu ganjil atau genap berdasarkan angka terbilang 1 sampai 10, contoh: satu, dua, tiga

Jawaban

package main

import (
	"fmt"
	"strings"
)

// soal no 1
func SwitchExercise2(score int) {
	var result string
	switch {
	case score >= 90 :
        	result = "A"
 	case score >= 80 && score < 89 :
        	result = "B"
	case score >= 70 && score < 79 :
        	result = "C"
    	case score >= 60 && score < 69 :
        	result = "D"
	default :
		result = "E"
	}
	
	fmt.Printf("Anda mendapatkan nilai %v \n", result)
}

// soal no 2
func CalculateTotal(price float64, qty float64, discount bool) {
	var total float64 = price * qty
	if discount {
		total = total * 0.9
	}
	fmt.Printf("\nTotal : %v \n", total)
}

// soal no 3
func CalculateVocalAlphabet(input string) {
	vocals := []string{"a" ,"e" ,"i" , "o", "u"}
	var totalVocal int = 0

	for i := range input {
		for j := range vocals {
			if strings.ToLower(string(input[i])) == vocals[j] {
				totalVocal++
			}
		}
	}
	
	fmt.Printf("Total huruf vokal pada string '%v' ada %d huruf vokal.\n", input, totalVocal)
}

// soal no 4
func CalculateFactorial(input float64) {
	var result float64 = input
	for i := 1; i < int(input); i++ {
		result = result * (input - float64(i))
	}

	fmt.Printf("Hasil dari faktorial %f adalah %f \n", input, result)
}

// soal no 5
func CheckOddOrEven(input string) {
	var number int
	switch {
	case input == "satu":
		number = 1
	case input == "dua":
		number = 2
	case input == "tiga":
	    number = 3
	case input == "empat":
	    number = 4
	case input == "lima":
	    number = 5
	case input == "enam":
	    number = 6
	case input == "tujuh":
	    number = 7
	case input == "delapan":
	    number = 8
	case input == "sembilan":
	    number = 9
	case input == "sepuluh":
	    number = 10
	default:
	    fmt.Println("angka yang kamu input salah")
	}

	if number % 2 == 0 {
		fmt.Printf("\nBilangan %v adalah bilangan genap", input)
	} else {
		fmt.Printf("\nBilangan %v adalah bilangan ganjil", input)
	}
}
PreviousSwitchNextLooping

Last updated 1 year ago