To create and use functions in Go, you can follow these steps:
func
keyword followed by the function name, parameter list, and return type. For example:func sayHello() {
fmt.Println("Hello!")
}
func functionName() returnType
syntax. For example:func sum(a int, b int) int {
return a + b
}
sayHello() // Call the sayHello function
result := sum(10, 5) // Call the sum function and store the result
fmt.Println(result)
func printMessage(message string) {
fmt.Println(message)
}
printMessage("Hello, World!") // Call the printMessage function with an argument
func calculate(a, b int) (int, int) {
return a + b, a - b
}
sum, diff := calculate(10, 5) // Call the calculate function and use multiple return values
fmt.Println(sum, diff)
Note that you need to import the necessary packages to use built-in functions from the Go standard library.