HomeAuthorsContact
GO and Functions

GO and Functions

By Frédéric Schmidt
May 21, 2020
1 min read

Definition

As in almost all languages, there is a way to encapsulate a set of instructions that performs a specific processing, it is the black box principle, with a set of input parameters and an output result. With the functions one abstracts from the detail of the implementation. It could be written by another person for example.

The term _routine_was also used.

The functions also make it possible to split and identify the different treatments.

Syntax

func <FunctionName>(<Param1> type, <Param2> type, ...) <TypeRetour> {

    // Implémentation
    
}

Example

The most glaring example is the main function of any GO program for the point of entry.

package main

import (
    "fmt"
)

func main() {
    var resultat int = 1
    var monTexte string = "Resultat: " + resultat
    
    fmt.Printf("%vn", monTexte)

}

Important: In terms of the visibility of a function, in this sense we are talking about public or private. This is done by either putting the first letter in lowercase for a private function and a capital letter for a public service.

Function Call

It is all well and good to have declared a function. It is necessary to call on its services. The example below is voluntarily detailed. This function takes an input parameter and returns an output parameter.

package main

import "fmt"

func main() {
    var name string = "John Doe"
    
    messageForYou := message(name)
    fmt.Println(messageForYou)

}

func message(nom string) string {
    return fmt.Sprintf("Hello dear %s.", nom)
}

messageForYou := message(name)
  • In summary

    And now you know:

    • Declare functions with input and/or output parameters;

    • Change the scope of the function in private or public which will be highly useful when writing packages for example;

    • Make the call has this function and retrieve its result.


Tags

#ProgramingLanguages#go#goland#function
Frédéric Schmidt

Frédéric Schmidt

Software Architect

I'm Software Developer & Architect. I'm more than 20 years experiences in differents IT service companies and software editors. I like studies some new technologies and share this passion altroughts my technical blog.

Expertise

Architecture
Development
Modelisation
Technical writer

Social Media

githublinkedin

Related Posts

GO and Packages
Programming Languages
GO and Packages
June 09, 2020
1 min
© 2022, All Rights Reserved.

Quick Links

Contact Us

About me

Social Media