func-go
path: courses/go-course/func-go.md
- **fileName**: func-go
- **Created on**: 2024-08-25 13:12:51
function in go like this:
// function taking to param int , and return int
func sub(x int, y int) int {
return x-y
}
can adding shourtcut for the function param example:
// the funciton taking 3 param of type int can adding new param for it like string
func sub(x, y , z int) int {
return x-y+z
}
// another example
func addToDatabase(hp, damage int, name string, level int) {
// ?
}
can saving the return value from function in variable
func getPoint() (x int, y int) { // function that take no param and return x and y
return 3, 4
}
// ignore y value
x, _ := getPoint()
automatic return value example:
func getCoords() (x, y int){
// x and y are initialized with zero values
return // automatically returns x and y
}
// like this
Is the same as:
func getCoords() (int, int){
var x int
var y int
return x, y
}
package main
import "fmt"
func main() {
x , y := returnValues()
fmt.Printf("value of x: %d, value of y: %d", x, y)
return
}
func returnValues()(x int, y int) {
x = 10;
y = 20;
return
}
named returns func example:
func calculator(a, b int) (mul, div int, err error) {
if b == 0 {
return 0, 0, errors.New("Can't divide by zero")
}
mul = a * b
div = a / b
return mul, div, nil
}
- Named return parameters are great for documenting a function. We know what the function is returning directly from its signature, no need for a comment.
- tips: When you choose to omit return values, it's called a naked return. Naked returns should only be used in short and simple functions.
explicit returns example:
// 1-
func getCoords() (x, y int){
return x, y // this is explicit
}
// 2-
func getCoords() (x, y int){
return 5, 6 // this is explicit, x and y are NOT returned
}
// 3-
func getCoords() (x, y int){
return // implicitly returns x and y
}
using go errors and fmt library making a divdie func to check for the error and return the value of divide x, y param and using errors package to handle the errors
package main
import (
"errors"
"fmt"
)
func main() {
err, x := devideNumber(20, 0)
if err != nil {
fmt.Printf("the error massage: %s" ,err)
return
}
fmt.Printf("the valuue of divide func: %d" , x)
return
}
func devideNumber( x, y int) (error , int) {
if y == 0 {
return errors.New("can't devide by zero sorry") ,0
}
return nil, x / y
}
example for callback function as param():
func reformat(message string, formatter func(string) string) string {
x := formatter(message)
return x
}
func formatter(message string)string {
return "TEXTIO: " + message + "..."
}
Anonymous Functions Example:
func main() {
newX, newY, newZ := conversions(func(a int) int { return a + a }, 1, 2, 3)
// newX is 2, newY is 4, newZ is 6
newX, newY, newZ = conversions(func(a int) int { return a * a }, 1, 2, 3)
// newX is 1, newY is 4, newZ is 9
}
- func() without name is anonmouse functions is using only for do job only one time like the code above the using only one time and can't using this function again
defer function using example:
package main
import ( "fmt")
func main() {
defer fmt.Printf("good bay from yossef\n")
fmt.Printf(" 1- foo\n")
fmt.Printf(" 2- bar\n")
return
}
/*
the defer key word using to execute thing when the function return value
output:
1- foo
2- bar
good bay from yossef
*/
- use cases for defer Defer is a great way to make sure that something happens before a function exits, even if there are multiple return statements, a common occurrence in Go.5
- Deferred functions are typically used to clean up resources that are no longer being used. Often to close database connections, file handlers and the like.
block scope
// scoped to the entire "main" package (basically global)
var age = 19
func sendEmail() {
// scoped to the "sendEmail" function
name := "Jon Snow"
for i := 0; i < 5; i++ {
// scoped to the "for" body
email := "snow@winterfell.net"
}
}
Blocks are defined by curly braces {}
. New blocks are created for:
- Functions
- Loops
- If statements
- Switch statements
- Select statements
- Explicit blocks
carry function is way to making function that take a function as param and return the function action or operation
func main() {
squareFunc := selfMath(multiply)
doubleFunc := selfMath(add)
fmt.Println(squareFunc(5))
// prints 25
fmt.Println(doubleFunc(5))
// prints 10
}
func multiply(x, y int) int {
return x * y
}
func add(x, y int) int {
return x + y
}
func selfMath(mathFunc func(int, int) int) func (int) int {
return func(x int) int {
return mathFunc(x, x)
}
}
continue:fmt.md
before:generics-go.md