SoFunction
Updated on 2025-03-05

Basic tutorials on go language (packages, variables and functions)

Bag

The go program consists of different packages, and the entry of the program is a package called main. For example, we create a main file

package main

import "fmt"

func main(){
    ("hello")
}

Go requires that the non-commented start of each go file must be ``` package xxx``, that is, which package the file belongs to.

Export method

First we create the following package folder:

My project 
├─mytool
│ ├─ alg
│ │  ├─ 
│ │  └─ 
│ └─ 
└─Other documents

That way, if we areNeed to use it insideFor the functions inside, you need to add such statements:

...
import "Project name/mytool/alg"
...
    ()
...

Two of the statements export the alg package and call the cmp function under the alg package.

Group import

Generally speaking, it is recommended to use group import packages, which is the writing method:

import(
    "fmt"
    "text/mytool/alg"
)

This is a not recommended way of writing, and it is not convenient to write:

    import "fmt"
    import "text/mytool/alg"

Exportable and non-exportable

Go's package supports non-exportable functions, just like private functions in a class, the declaration is whether the first letter of the function name is case or not.

Add the following two functions to the file:

func action() {
	("action")
}
func Move(){
    ("move")
}

Among them, only the Move function can be called, while the action function cannot be called because the initial letter is not case-censored, because it is not exported.

function

Defining functions in go requires using the func keyword

parameter

The function of go can have no parameters or multiple parameters, as follows:

func novar(){

}
func manyvar(a int, b int){

}

If the parameter types are consistent, you can only write the type of the last parameter, as follows:

func manyvar(a,b int){

}

Return value

If the go function has a return value, it needs to be declared after the parameter, as follows:

func havereturn() int{

}

It will return a return value of type int

The return value can also have a specified name, and the purpose is to increase the readability of the code, as follows:

func sum(a, b int) (ans int){
    ans = a + b
    return ans
}

If a function is very long, then determining the name of the return value at the beginning will greatly increase readability

go supports functions to return multiple return values, as follows:

func manyres() (int, error){
    return 1 , nil
}

It returns two return values ​​of int and error types, respectively

variable

Variables in go are created using the var keyword, and the variable type is added after the variable name, as follows:

var a int

This code creates a variable of type int

Variables can also be assigned and initialized by equal signs like other languages, as follows:

var a = 1//This is called initialization
//You can also initialize multiple values ​​at oncevar b, c = false, "AMDYES!"

a = 2//This is called assignment

What should I do if I don't want to write the var keyword? This can be written in the function:

func text(){
    a := 1//This is also equivalent to creating and initializing variables}

This is not allowed to be written outside the function. Go stipulates that each statement outside the function must start with a keyword.

If the initialization does not have a clear initial value in go, the unified assignment is to the default zero value in go

The type conversion method in go is T (variable), and T represents a variable type
as follows:

var a int = 1
var b = float64(a)

Go's type derivation will be triggered when you declare a variable without specifying the variable type. It determines the type of the variable based on the result type expressed on the right, as follows:

var a int =1
b := a//The type of b is deduced as intc := 1//At this time c is deduced as int, that is, the constant integer is int in god := 1.1// At this time d is deduced as float64e := 1 + 1i//This iscomplex128

The constant declaration in go is passed through the const keyword, as follows:

const a int = 1
const b = 1.1//You can also not write the type go Automatic derivation

But it should be noted that:=Only variables can be declared, constants cannot be declared using this

This is the article about the basic tutorial on Go language (packages, variables and functions). For more related Go language packages, variables and functions, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!