go functions

// a simple function
func functionName() {}

// function with parameters (again, types go after identifiers)
func functionName(param1 string, param2 int) {}

// multiple parameters of the same type
func functionName(param1, param2 int) {}

// return type declaration
func functionName() int {
    return 42
}

// Can return multiple values at once
func returnMulti() (int, string) {
    return 42, "foobar"
}
var x, str = returnMulti()

// Return multiple named results simply by return
func returnMulti2() (n int, s string) {
    n = 42
    s = "foobar"
    // n and s will be returned
    return
}
var x, str = returnMulti2()

3.67
3

                                    package main

import "fmt"

func add(int x, int y) int {
	return x + y
}

func main() {
	fmt.Println(add(42, 13))
}
Notice that the type comes after the variable name

3.67 (3 Votes)
0
4.13
8
DiDomenico 105 points

                                    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())
}

4.13 (8 Votes)
0
Are there any code examples left?
New code examples in category Go
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
functional programming golang golang go func() go func() means golang function structure go functions explained go function example syntax of a function in go golang function in structure call functions in go function in for golang what is go function golang function this golang call a function golang go func tutorial function go lang fun(*) in golang function programming golang make a function with golang function func in func golang how to write function in golang go function definition declare function golang golang go function go language functions go function declaration go func(){} go func() define function in go golang define function go function calling functional programming in golang golang function example func in golang define function golang golang function or method go function syntax func golang go function call how to make a function in go what is a function in golang how do you call a function in golang function example go how to call functions in go golang func calling functions in go how to use function in golang call function golang function in golang golang function go functions in golang golang function in function golang go func golang function variable how to run a function in go what is go func() in golang make function in golang golang functional programming make function golang go func keyword golang go func golang func() go golang make function define function in golang function in function golang functions in go go func go functions why have * func (* go function go function golang function in go go call function go make function go function go ffunctions where do create function go golang function tutorial go or function call function go go functions
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source