Go language Hello World instance _golang

Source: Internet
Author: User
Tags in python

Before we start writing apps, let's start with the most basic programs. Just as you don't know what a foundation is before you build a house, writing a program doesn't know how to start. So in this section, we're going to learn to run the Go program with the most basic syntax.

Program

It's like a tradition, before you learn most languages, you learn how to write a program that can output Hello world.

You ready? Let ' s go!

Copy Code code as follows:

Package Main

Import "FMT"

Func Main () {
Fmt. Printf ("Hello, world, or Hi, Worlds orκαλημ́ρακóσμorこんにちはせかい\n")
}

The output is as follows:

Copy Code code as follows:

Hello, World or Hi, orκαλημ́ρακóσμorこんにちはせかい.

Detailed

First we need to understand the concept that go program is organized through package.

Package <pkgName> (in our case, package main) tells us which package the current file belongs to, and the package name main tells us that it is a stand-alone package that produces an executable file after it is compiled. In addition to the main package, the other packages eventually generate the *.a file (that is, the package file) and place it in the $gopath/pkg/$GOOS _$goarch (in the case of Mac $gopath/pkg/darwin_amd64).

Copy Code code as follows:

Each of the go programs that can run independently must contain a package main, which in this main package must contain an entry function main, which has no parameters and no return value.

To print Hello, world ..., we called a function printf, which came from the FMT package, so we imported the system-level FMT package in the third line: import "FMT".

The concept of packages is similar to the package in Python, and they all have some special benefits: modularity (the ability to split your program into multiple modules) and reusability (each module can be reused by other applications). We are here to understand the concept of the package first, and later we will write our own package.

In line fifth, we define a main function with the keyword func, and the function body is placed in {} (curly braces), just as we normally write C, C + +, or Java.

You can see that the main function doesn't have any arguments, and then we'll learn how to write a function with parameters that returns 0 or more values.

Line six, we called the FMT package inside the definition of the function printf. As you can see, this function is called by the way of <pkgName>.<funcName>, which is very similar to Python.

Copy Code code as follows:

As mentioned earlier, the package name and the folder name of the package can be different, where <pkgName> is the name of the package that is declared through package <pkgName>, not the folder name.

Finally, you can see that the content we output contains a lot of non-ASCII code characters. In fact, go is a natural support UTF-8, any character can be directly output, you can even use any character in UTF-8 as an identifier.

Conclusion

Go uses package (similar to Python's modules) to organize your code. The Main.main () function (which is primarily located in the main package) is the entry point for each individual programmable program. Go uses UTF-8 strings and identifiers (because the inventor of the UTF-8 is the inventor of go), so it is inherently multilingual.

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.