The HelloWorld of the Go language under the MacBook

Source: Internet
Author: User
Tags macbook

  • Directory for Go language development

    General Go Language $gopath directory conventions have three subdirectories:
      • SRC store source code (e.g.,. Go. C. h, etc.)
      • The files generated by the PKG after compilation (for example:. a)
      • Bin compiled executable file (for convenience, this directory can be added to the $PATH variable, if there are multiple gopath, then use to ${GOPATH//://bin:}/bin Add all bin directory)
  • Go Language Development Directory planning

    Gopath under the SRC directory is the main directory of the next development program, all the source code is placed under this directory, then generally we practice is a directory of a project, for example: $GOPATH/src/mymath represents mymath this application package or executable application, This is based on whether the package is main or otherwise, and the main word is the executable application, and the other is the app

  • Creation of HelloWorld
    Let's take MyMath as an example of how to write an app package and execute the following code:
    CD $GOPATH/srcmkdir MyMath

    Create a new file Sqrt.go with the following content:

    Package Mymathfunc Sqrt (x float64) float64 {z: = 0.0for I: = 0; i <; i++ {z-= (z*z-x)/(2 * x)}return Z}

    Note: It is generally recommended that the package name and directory name remain the same

    At this point, the app package has been created, and the following is the installation of the app package, as in the next two ways:

    1, just enter the corresponding application package directory, and then execute go install , you can install the

    2. Execute the following code in any directorygo install mymath

    After the installation is complete, pass the command:

    CD $GOPATH/pkg/${goos}_${goarch}

    You can see the file mymath.a

    At this point, the application package has been established, a program to invoke the application package, a new application package Mathapp

    CD $GOPATH/srcmkdir mathappcd mathappvim main.go

    The source code of Main.go is as follows:

    Package Mainimport ("MyMath" "FMT") func main () {FMT. Printf ("Hello, World.  ") SQRT (2) =%v\n ", MyMath. SQRT (2))}

    Go to the app's directory and the Go build will generate a Mathapp executable that executes the following command:

    ./mathapp

    You will get the following output:

    Hello, world.  SQRT (2) = 1.414213562373095

   

The HelloWorld of the Go language under the MacBook

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.