Added new Examples to use as templates
This commit is contained in:
parent
bcc4a48e08
commit
e6f4dc5cc8
4 changed files with 263 additions and 0 deletions
62
Example.go
Normal file
62
Example.go
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func test() {
|
||||
// Example of printing to the console
|
||||
fmt.Println("Hello, World!")
|
||||
|
||||
// Basic math operations
|
||||
num1 := 10
|
||||
num2 := 5
|
||||
|
||||
// Addition
|
||||
sum := add(num1, num2)
|
||||
fmt.Printf("Sum: %d\n", sum)
|
||||
|
||||
// Subtraction
|
||||
difference := subtract(num1, num2)
|
||||
fmt.Printf("Difference: %d\n", difference)
|
||||
|
||||
// Multiplication
|
||||
product := multiply(num1, num2)
|
||||
fmt.Printf("Product: %d\n", product)
|
||||
|
||||
// Division with error handling
|
||||
quotient, err := divide(num1, num2)
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
} else {
|
||||
fmt.Printf("Quotient: %f\n", quotient)
|
||||
}
|
||||
|
||||
// Example of using a loop
|
||||
for i := 0; i < 5; i++ {
|
||||
fmt.Printf("Loop iteration %d\n", i)
|
||||
}
|
||||
}
|
||||
|
||||
// Function to add two integers
|
||||
func add(a, b int) int {
|
||||
return a + b
|
||||
}
|
||||
|
||||
// Function to subtract two integers
|
||||
func subtract(a, b int) int {
|
||||
return a - b
|
||||
}
|
||||
|
||||
// Function to multiply two integers
|
||||
func multiply(a, b int) int {
|
||||
return a * b
|
||||
}
|
||||
|
||||
// Function to divide two integers, returns an error for division by zero
|
||||
func divide(a, b int) (float64, error) {
|
||||
if b == 0 {
|
||||
return 0, fmt.Errorf("division by zero")
|
||||
}
|
||||
return float64(a) / float64(b), nil
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue