3 minutes to get you started with Go language (nanny level tutorial)

Posted by watsmyname on Sun, 23 Jan 2022 16:58:59 +0100

Go language introduction detailed knowledge, mainly to share with you go language composition, go language function format, C language to go language comparison.

Go language program composition

  • Like C language program, Go language program is also composed of many functions

  • Like C language programs, the system * automatically calls a function called main when the program runs

  • Like C language programs, if a program does not have a main function, the program does not have the ability to run

  • Like C language programs, a Go language program has and can only have one main function

Go language program main function definition format

  • C language main function format

int main(int argc, const char * argv[]) {
    return 0;
}
  • Go language main function format

    • func tells the system that this is a function

    • Main main function fixed name

    • The left parenthesis of a function must be on the same line as the function name

    • The main function must be in the main package

//Tell the system which package the currently written code belongs to
package main
//Defines a function called main
func main() {
}

Go language HelloWorld

package main //Tell the system that the current code belongs to the main package
import "fmt" //Import fmt package corresponding to print function
func main() {
        //By package name Function name, using the print function in fmt package to output statements
 fmt.Println("Hello World!!!")
}

Similarities and differences between Go language HelloWorld and C language HelloWorld

  • 1. Different file types

    • c language code is saved in c is the suffix of the file

    • The go language code is saved in In files with go suffix

  • 2. Different code management methods

    • We will put different types of code into different go file, and then assign a package name to the file through package

    • If necessary, you can directly import the corresponding package name through import

    • c language will put different types of code into different c file, and then write the corresponding h file

    • If necessary, import the corresponding files directly through #include h file is enough

    • C language programs manage code in the form of files

    • Go language program manages code in the form of package

  • 3. The main function writing file is different

  • The main function in C language can be written in any file, as long as a program has only one main function

  • In Go language, the main function can only be written in the folder named main in the package. Similarly, a program needs to be saved, and there is only one main function

  • 4. Functions are written in different formats

    • Note: the left bracket of C language function can be on the same line as the function name or not

    • The format of function in C language is

return type Function name(parameter list ) {
        Function body related statements;
        return Return value;
}
    • Note: the left bracket of Go language function must be on the same line as the function name, otherwise an error will be reported

    • Go language function definition format

func  Function name(parameter list )(Return value list) {
        Function body related statements;
        return Return value;
}
  • 5. The format of function call is different

    • C language is imported through #include h file, call the function directly through the function name

    • After the Go language imports the corresponding package through import, it needs to pass the package name Called by function name

#include <stdio.h>
#include "calculate.h"
int main()
{
    int res = sum(2, 3); //Call the function directly with the function name
    printf("res = %d!\n", res);
    return 0;
}
package main
import (
 "fmt"
 "lesson_1/calculate"
)
func main() {
 res := calculate.Sum(2, 3) //Use package name Function name calling function
 fmt.Println("res1 = ", res)
}
  • 6. The statement ends differently

    • Every statement in C language must end with a semicolon

    • There is no need to add a semicolon after each statement in Go language (the compiler will add it automatically)

#include <stdio.h>
#include "calculate.h"
int main()
{
    int res = sum(2, 3); //If you don't write a semicolon, you will report an error
    printf("res = %d!\n", res); //If you don't write a semicolon, you will report an error
    return 0; //If you don't write a semicolon, you will report an error
}
package main
import (
 "fmt"
 "lesson_1/calculate"
)
func main() {
 res := calculate.Sum(2, 3) //Don't write semicolons
 fmt.Println("res1 = ", res) //Don't write semicolons
}

Go language notes

  • Like C language, Go language also supports single line comments and multi line comments, and the characteristics of all comments are the same as C language

    • Single line comment / / annotated content

    • Multiline comment / * annotated content*/

  • In the Go language, the official recommends using single line comments rather than multi line comments (for details, you can directly view the official Go source code)

Topics: Go