Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • Go lang - Get Started
  • Go lang Overview
  • Install Go
  • Go Syntax
  • Create Go Program
  • Go DataTypes
  • Go Variables
  • Go Operators
  • Go if-else Statement
  • Go Switch Statement
  • Go For Loop
  • Working with Functions
  • Go Arrays
  • Go Slice
  • Go Struct
  • Go Map
  • Go Pointers
  • Go Packages
  • Go Modules
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Working with Functions in Go

In Go, the function is a set of statements that performs a specific task and can be reused repeatedly in a program. Functions improve the readability and maintainability of a program.

A function can take input parameters and can also return an output. Input parameters and output are optional to a function.

Every Go program will have at least one function main() function which is the starting point.

Syntax:
func functionName ([inputParameters]) [returnType] {
	//function body
}

A function in Go is declared using the func keyword followed by the function name. Input parameters can be passed to a function which is optional. A function can return one or more values. In the above syntax, returnType is the data type of the return values. It's not mandatory to return a value, in that case, the returnType is not required. The function body is a set of executable statements that performs a specific task.

The following example defines the greet() function:

Example: Function
func greet() {
	fmt.Println ("Hello World") 
}

In the above example, a function greet() is defined using the func keyword. The opening curly brace { indicates the beginning of the function. It should be placed in the same line as the function name declaration and not in a separate line. The closing curly brace indicates the end of the function.

The above greet() function does not take any input parameter and does not return any value. The function body contains single statement.

The function does not get executed automatically. It has to be explicitly called by using the function name followed by parenthesis e.g. greet().

A function can be called one or more times from any other functions.

Example: Function Call
package main
import "fmt"

func greet() {
	fmt.Println ("Hello World") 
}

func main() {
	 greet() // calling the greet function
}
Try it
Output:
Hello World

Function with Input Parameters and Return Value

In the following example, a function called Sum is declared which accepts two values as input parameters and returns the total of the two values.

Example: Function with Input Parameter and Return Value
package main
import "fmt"

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

func main() {
	result := Sum(5, 10)
	fmt.Println (result) 
}
Try it
Output:
15

In the above program, the Sum function takes two integers parameters, x and y. It computes the sum of these two integers and returns the result to the calling function.

Multiple Return Values

A function can return multiple values of different data types. For example the following function returns two integer values.

Example: Multiple Return Values
package main
import "fmt"

func SumAndSubtract (x int, y int) (int, int) {
	sum := x + y
	sub:= x – y

	return sum, sub
}

func main() {
	 
	sum,subtract := SumAndSubtract(10, 5)
	
	fmt.Println(sum, subtract) 
}
Try it
Output:
15 5

A function can return multiple values of different data types.

Example: Return Multiple Values of Different Data Types
package main
import "fmt"

func GetDefaults() (int, string) {
	return 0, "unknown"
}

func main() {
	id, name := GetDefaults()
	
	fmt.Println(id, name) 
}
Try it
Output:
0 unknown

Naming Conventions for Functions

  • A function name must start with a letter.
  • A function name cannot contain spaces.
  • It can contain only alphanumeric characters and underscore.
  • Function names are case sensitive, e.g. Greet() and greet() are two different functions.
TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.