Skip to content

Instantly share code, notes, and snippets.

@Thedrogon
Last active April 28, 2025 15:17
Show Gist options
  • Save Thedrogon/575580a2a421c478351762668c55e03c to your computer and use it in GitHub Desktop.
Save Thedrogon/575580a2a421c478351762668c55e03c to your computer and use it in GitHub Desktop.
A Gist that shows how functions , Conditional statements , Loops in Go
// Function in golang where we pass 2 values to get the added value
func add (a int ,b int) (int,int) {
return a+b,a*b
}
//In golang to export a function or variable , struct or function u can just capotalise the initial letter of that member
func Add (a int ,b int) (int,int) {
return a+b,a*b //NOW IN THIS CASE IT BECOMES EXPORTABLE FUNCTION.
}
fuc main(){
sum , product := add(3,4) //In go return types are treated like first class citizens .
sum1 , _ := add(6,7) //when we dont wanna accept bot the values we can simply use "_" in place of a variable.
fmt.Printf("This is sum = %d and this is product = %d",sum,product)
//Conditional statements in go are just like any other language
if sum > 50 {
fmt.Printf("This is sum = %d and its greater than 50",sum)
}else if product < 100{
fmt.Printf("This is product = %d and it is less than 100",product)
}
//Switch cases in golang are a bit different , theres no break statement
//keyword fallthrough is used for executing the next all use cases after the keyword
day := "tuesday"
switch day {
case "Monday" : fmt.Println("Its Monday ")
case "tuesday" : fmt.Println("Its Tuesday ")
case "wednesday" :
fmt.Println("Its Wednesday ")
fallthrough
case default : fmt.Println("Its allweek ")
}
//Loops and iterations
for i:=0 : i< 5 ; i++{ //A simple for loop
fmt.Print("Running A for loop")
}
//in Go , a while loop is simply a for loop with single condition
counter := 0
for counter < 7 {
fmt.Print("This is a simple while loop")
counter++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment