Answers for "golang function variable"

Go
6

golang function

package main

import "fmt"

func basicFunction() {
	fmt.Println("basic function golang")
}

var variableFunction = func() {
	fmt.Println("basic function golang with variable function")
}

func parameterFunc(a, b int) {
	addition := a + b
	fmt.Println("total", addition)
}

func returningFunc(a, b int) int {
	addition := a + b
	return addition
}

func multipleReturningFunc(a, b int) (string, int) {
	multiple := a + b
	return "nilainya adalah", multiple
}

func multipleReturningNamingFunc(a, b int) (multiple, subtract int) {
	multiple = a * b
	subtract = a - b
	return
}

func variadicFunction(array ...int) (sum int) {

	for _, v := range array {
		sum = v
		fmt.Println(sum)
	}

	return
}

func anonymousFunc() {
	name := "john doe"
	func() {
		fmt.Println(name)
	}()
}

func anonymousParamsFunc() {
	name := "jane doe"
	func(str string) {
		fmt.Println(str)
	}(name)
}

func closureFunc() func() string {
	name := "john"
	return func() string {
		name += "doe"
		return name
	}
}

func main() {
	clousure := closureFunc()

	basicFunction()
	variableFunction()
	parameterFunc(10, 2)
	fmt.Println("total", returningFunc(10, 10))
	fmt.Println(multipleReturningFunc(40, 2))
	fmt.Println(multipleReturningNamingFunc(10, 2))
	variadicFunction(1, 2, 3, 4, 5)
	anonymousFunc()
	anonymousParamsFunc()
	fmt.Println(clousure())
	fmt.Println(clousure())
	fmt.Println(clousure())
}
Posted by: Guest on March-08-2021
0

golang pointer

package main

import "fmt"

type Person struct {
	Firstname string
	Lastname  string
}

func pointerParameter(fullname *string) {
	*fullname = "john doe"
	fmt.Println("passing data with pointer", *fullname)
}

func pointerStructParameter(person *Person) {
	fmt.Println(*person)
}

func pointerStructParameterWithRetruning(person *Person) (pointer interface{}) {
	pointer = *person
	return pointer
}

func main() {
	var fullname string

	realNumber := 5
	pointerNumber := &realNumber
	number := pointerNumber
	person := &Person{
		Firstname: "john doe",
		Lastname:  "margareth",
	}

	var yearsPointer *int
	years := &yearsPointer

	pointerParameter(&fullname)
	pointerStructParameter(person)

	fmt.Println("real number", realNumber)
	fmt.Println("pointer number", pointerNumber)
	fmt.Printf("real number from pointer %vn", *number)
	fmt.Println("nil pointer", *years)
	fmt.Println("read pointer paramter with returning", pointerStructParameterWithRetruning(person))

}
Posted by: Guest on March-10-2021

Browse Popular Code Answers by Language