Go lang preliminary interview Hello World

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

First go lang program--hello world

Package Mainimport "FMT" Func Main () {p ("Rocky")}/* block comment */func p (name string) {FMT. Println ("Hello," + name)}//single line comment//func p () {//FMT. Println ("Hello world!") // }

Some simple grammatical features

The simple code above uses some of the following features of Go lang:

    1. The first line of the code is the package; The above example, a program that runs separately using a single file must be placed under packages main, otherwise the go run runs with an error: "Go run:cannot run Non-main"

    2. Import package name must be quoted, multiple package names can use multiple import, you can enclose the package name in quotation marks, connect with a newline character, and wrap the parentheses around the outermost layer;

    3. Import cannot reference a package that is not used in the code, otherwise the compilation will error: "Imported and not Used:xxx"

    4. "{" must follow the code and not appear in the new line;

    5. Main as a program entry, can not carry parameters, command line parameters using the OS. The args variable gets;

    6. Note there are block comments/* */and single-line comments//two kinds;

    7. Define the func format: Func func_name (input parameter) (return parameter--can be omitted) {};

    8. Func definition does not support overloading (overload)

"The Pit" in Hello World

Don't underestimate the above lines of code, a lot of pits:

    1. Package is not main, resulting in a failure to run

    2. Import FMT does not use double quotes "" Package name, resulting in inability to run

    3. Understanding and use of Go run/go install/go build, etc.


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.