Golang Study notes (1)---Go program general structure

Source: Internet
Author: User

Go programs are organized through the package (similar to the Python library)

Only packages with a package name called Main can contain the main function (and the main function is also the entry for a program)

One executable program has and only one main package

Importing other non-main packages via the import keyword

Constants are defined by the const keyword

Declaring and assigning global variables by using the var keyword outside the body of the function

Declaration of a struct (struct) or interface (interface) using the type keyword----general (custom) type

Declaring a function with the func keyword


The General format is:

Packages main//current package name, only the package name is main, you can generate the executable file import "FMT"//Introduce a package import ("FMT" "OS")//here with parentheses, you can introduce more than one package at a time, of course, you can import each Times to introduce one. (The introduction does not use, the compile time will be error) const PS = 3.14//const used to define a constant var name = "Lixin"//var used to define a variable, is generally the declaration and assignment of a global variable type Newtype int//type with To declare a type, followed by an int, representing the general type of declaration of type Gopher struct{}//followed by a struct representation is a struct type, and of course there are some parameter definitions, omitted here so with {}type Golang interface{}// Represents the declaration of an interface, Func main () {//by the main function as the starting point of the program FMT. Println ("Hello World")//Call the PRINTLN function of the FMT package to output the string}


Tips for referencing:
Import "FMT" Here I can alias the imported package to set the import test "FMT" then I call FMT below. The println function is: test.println We can also make an ellipsis call--Generally not!! Import. "FMT" before the addition of a dot to indicate the omitted call, then call the module inside the function, you can not write the module name of import. "FMT" Func Main () {Println ("Hello,world")}


This article is from the "Your Night" blog, be sure to keep this source http://lixin15.blog.51cto.com/3845983/1846826

Golang Study notes (1)---Go program general structure

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.