Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the pitfalls that Go language needs to pay attention to?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you the "Go language need to pay attention to what are the holes", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn the "Go language need to pay attention to what are the holes" this article.

Basic syntax of GO language

The following is the basic structure of a Go program, including (package declaration, introduction package, function, etc.)

Package main / / defines the package name, and package main represents a program that can be executed independently, and each Go application contains a package named main. Import "fmt" / / imports the entry function of the package (or other element) func main () {/ / program that you need to use. The main function must be included in every executable program, and it is generally the first function to be executed after startup. Fmt.Println ("Hello, World!")}

The pit of attention in GO language

No matter what you learn, you will encounter all kinds of pitfalls at the beginning. Here is a summary of the various pitfalls encountered in the process of learning the go language.

1. People who write C # will put "{" on a separate line, but this is an error in go. "{" must be on the same line. I made this mistake the first time I wrote go, and I don't know where it is.

Func main () {fmt.Println ("Hello, World!")}

2. If... The else in the else statement must be on the same line as the'}'of if, otherwise there will be a compilation error

Var an int = 30 if a < 20 {fmt.Print ("axiom 20")}

two。 The definition of the package name. You must declare the package name in the first line of the source file that is not commented, such as: package main. Package main represents a program that can be executed independently, and each Go application contains a package called main.

Package main

3. In a Go program, a line represents the end of a statement. Each statement does not need to end with a semicolon like other languages in the C family, because the work will be done automatically by the Go compiler.

If you plan to write multiple statements on the same line, you must use it; artificially distinguish, but in actual development we do not encourage this practice.

Fmt.Println ("Hello, World!") fmt.Println ("www.fpeach.com")

3. The main () function must be included in every executable program, and it is generally the first function to be executed after startup. However, there can be only one main () function in each package, otherwise it will report main redeclared in this block previous declaration at.. Donovan's mistake.

Package main

Import "fmt"

Func main () {/ * this is my first simple program * / fmt.Println ("Hello, World!")}

4. When functions, structures, and other identifiers begin with an uppercase letter, such as GetInfo, then objects that use identifiers in this form can be used by external package code, which is called export (like public in object-oriented languages); identifiers that start with lowercase letters are not visible outside the package, but they are visible and available inside the entire package (like protected in object-oriented languages).

/ / Public functions that can be used by the code of the external package

Func Test () {.

.

.}

/ / Private function, the inside of the package is visible,

Func test2 () {.

.

.}

5. Identifiers are used to name variables, types, and other program entities. An identifier is actually a sequence of one or more letters (AbeliZ and axiz) numbers (0,9) and underscores _, but the first character must be a letter or underscore, not a number.

The following are invalid identifiers:

1ab (starts with a number)

Case (keyword of Go language)

Aaccounb (operator is not allowed)

6. Error no new variables on left side of: =, which means, "there is not a single new variable on the left!"

Func main () {var b int = 20 b: = 30 fmt.Print (b)}

The solution is: for xonomer yizhuangfu. In this form, just name one of the variables as new.

7. Variables cannot be initialized and assigned using the + + self-increment or-- self-subtraction operators

Package mainimport "fmt" func main () {var an int = 10 var b int = ahe + var c int = 20c = ahe + fmt.Print (a, b, c)} these are all the contents of this article entitled "what are the pitfalls that Go language needs to pay attention to". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report