Learning Golang Language (1): Hello World

Source: Internet
Author: User
Tags learn golang
This is a creation in Article, where the information may have evolved or changed.

Today began to enter the Golang language learning, Golang language installation and testing is no longer explained here. There is a lot of information on the network to reference.

Learning Golang Language (1): Hello World

Learning Golang Language (2): Variables

Learning Golang Language (3): Type--Boolean and numeric types

Learning Golang Language (4): Type--string

Learning Golang Language (5): Type--array

The first program to learn any programming language is Hello World, and I will not break this tradition. Look at Go's "Hello World".

Package Main
"FMT"//introduction of FMT Library
Func Main () {
Fmt. Println ("Hello world!")
}

Analyze This procedure line by row:

The first line is required. All files written in the Go language start with the package <***>, and the executable file must be the package main for the standalone run;

The second line indicates that the FMT package is added to main. Other packages, which are generally non-main, are called libraries,

The third line is the main function in the program. When the Go program executes, the function that is called first is the main function. This is inherited from C. Here is the function definition of the main function.

Line four calls the FMT package function to print the string to the screen. The string is wrapped by "" and can contain non-ASCII characters.


Note :

A standalone executable Golang program, the package main must appear, immediately following the introduction of the various libraries, and then the various functions, here must have a main function. The main function is the entry for the program.


Compiling and running

Use the command: Go build helloworld.go compile. The HelloWorld executable will be generated in the same directory.

Run:./helloworld

On-screen output: Hello world!

You can also use some parameters at compile time to reduce the size of the compiled file.

go build-ldflags "-s-w" helloworld.go (for reducing the size of the executable program generated by Golang compilation, please reply to 3 view (once slimming for Golang program ) )

The next section will describe the variables, types, and keywords of the Golang language.

--------------------------------------------------------------------------------------------------------------- --

Welcome to follow the code, learn Golang language together.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.