First Go Program

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

Here is a simple record of learning Golang journey, convenient for you to review.

Package Main

Import "FMT"// we need to use FMT in the package Println () function

func Main () {
Fmt. Println ("Hello, world! ")

}


eachGothe beginning of the source code file is a Packagedeclaration, which indicates that theGothe package to which the code belongs. Package isGoThe most basic distribution unit in the language is also the embodiment of the dependency relationship in engineering management. To generateGoexecutable, you must establish a name ofMainPackage , and contains a packet calledMain ()function (the function isGothe execution starting point of the executable).

Go language of Main () a function cannot have parameters, nor can it define a return value. The command line passes in the parameters in the os. Save in the Args variable. If you need support for command-line switches, you can use the flag package.

import statement, Used to import the package on which the program depends. Because this example program uses the println () function, so you need to import the FMT package.  

One thing to be aware of is not to include packages that are not used in source code files, otherwise go The compiler will report a compilation error. This is associated with the following forced left curly brace { go Language at the language level to solve the design philosophy of software engineering problems.  

all go function ( Includes type member functions that are mentioned in object programming) with the keyword func

func Name of function ( parameter list ) ( return value List ) {// function Body

}

A corresponding example is as follows:

func Compute (value1 int, value2 float64) (Result float64, err Error) {// function Body

thego supports multiple return values. The example function above Compute () returns two values, one called resultand the other is

err result Span style= "FONT-SIZE:10.000000PT; font-family: ' fzssjw--gb1-0 ' "will be set to 0.0 err Span style= "FONT-SIZE:9.000000PT; font-family: ' Courier ' ">nil

Go code comments for the program and C + + consistency, which supports the following two uses:

/*

Block annotations

*/

// Line Comment

believe in familiarCand theC + +the reader has also found another point in this paragraphGoThe semicolon is not present in the sample code. GoThe program does not require the developer to add a semicolon after each statement to indicate the end of the statement, which isCand theC + +one of the obvious differences.


Compile the program:

assuming that the previous introduction Hello, World The code is saved in order to Hello.go , and is located ~/golang directory, you can compile and run the program directly with the following command line:

$ CD ~/goyard
$ go Run hello.go # Direct running Hello,world!

using this command, you will compile, link, and run 3 step is merged into one step, and you will not see any intermediate and final executables in the current directory after you have finished running it. We can also use the build command of the Go command line tool if we want to build only the results of the compilation without running automatically:

$ CD ~/golang
$ go Build hello.go
$./hello
Hello,World!


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.