Go Bible-Getting started with learning notes

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

Hello,world

The go language does not need to include a semicolon at the end of a statement or declaration, unless there are multiple statements on the previous one. In fact, the compiler will actively convert a newline character after a particular symbol to a semicolon, so the position added by the line break will affect the correct parsing of the go code.

In the following example, the end of the line , causes the compiler to not add a semicolon, then this FMT. printf is a complete line, so it's more beautiful to write. You can't put it , on the next line.

fmt.Prinf("hello, %s。Welcome to %s",     name, country)

If you want to integrate an IDE under a Linux vim editor, you need to install a vim-go plugin. If the installation is OK, it will look like my environment, as follows:

There are several ways to declare a variable:

temp:=""var temp string = ""var temp = ""var temp string

Emphasis on the first way, it is the most concise, but only for functions or methods inside, do not apply at the package level of the variable declaration

package mainimport (    "fmt")temp:=""func main(){    fmt.Println(temp)}

Compile cannot pass, error messageexpected declaration, found 'IDENT'

Precautions

When programming with the go language, developers should try to use the official standard library, avoid repeating the wheel, improve the development efficiency, while running the program in time and space complexity of the better.

Here's a demo of stitching strings:

package mainimport (    "fmt")func main(){    s, sep:= "", ""    // 第一种方式:    for _, arg:= os.Args[1:]{        s+=sep+arg        sep = " "    }    fmt.Println(s)        // 使用官方标准库的方式    fmt.Println(strings.Join(os.Args[1:], " "))}

Post the official strings. Part of the code implementation of the Join standard library method:

func Join(a []string, sep string) string{    // 省略部分代码片段    n:= len(sep)*(len(a)-1) // sep所占用空间的整个长度    for i:=0;i<len(a);i++{        n+=len(a[i])    }        b:= make([]byte, n)    bp:=copy(b, a[0])    for _, s := range a[1:]{        bp+=copy(b[bp:], sep)        bp+=copy(b[bp:], s)    }    return string(b)}

Summary: Use of the standard library saves memory space and time. It is a one-time allocation of memory space, do not need to allocate and free memory repeatedly, memory allocation may occur in memory page size is not enough, resulting in the case of missing pages, it takes a lot longer

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.