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 common pitfalls in Go language development

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

Today, I would like to share with you the relevant knowledge points about the common pitfalls in Go language development, which are detailed in content and clear in logic. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.

As a simple and dexterous language, Go is deeply loved by developers. But for beginners, if you want to control it easily, you have to do a good job of learning details.

What beginners should pay attention to:

Curly braces cannot be made into separate lines.

Unused variable error-for global variables and function parameter variables, they can be defined and not used. However, for internal variables of a function, the compiler will prompt an error if it is not used after definition.

Import package uncalled error-after importing the package, if you do not call objects such as functions, interfaces, structures, and variables, a compilation error will occur. It is recommended that you use the white space symbol "_" to avoid similar errors.

Variable shorthand applies only to the interior of the function.

Redefine a variable using an abbreviated declaration-you cannot redefine a variable in a separate statement, but it is allowed in the case of compound variables. The redefined variable must exist in the same block, otherwise a shadow variable will be generated.

Unexpected variable-variable shorthand syntax is so simple that it is easy to use it as a regular assignment operation. Although it can be compiled, the result is not what we want.

You cannot use "nil" to define an untyped variable-the "nil" keyword can be used to represent a "0 value", such as in interfaces, functions, pointers, and so on. However, if it is used to define a variable that does not make a type declaration, it will result in a compilation error because the compiler cannot determine the variable type.

Use "nil" slices (Slices) and diagrams (Maps)-- it's okay to add objects to "nil" slices, but for diagrams, this makes it slow.

Graph capacity-you can set the capacity of the graph at creation time, but you don't have to use cap () on the diagram.

The string cannot be "nil".

Array function parameters-for the Cpicket + developer, the array is like a pointer; when the array is passed into the function, the function references the same memory location, so the original data can be updated. But in Go, arrays are values, so when you pass an array into a function, you get a copy of the original array data. So if you want to update the array data, this creates a problem.

Exceptions in slices and arrays "range"-this problem can be encountered when using for-in or foreach statements. In Go, the definition of range is different. It produces two values: the first is the index item and the second is the data of the item.

Single-dimensional slices and numbers-to create a dynamic multi-dimensional array, use separate slices. First, you create an outer slice; then, you assign each inline slice. Each inline slice is independent for other slices.

Access a graph key that does not exist-if you want to know whether a given graph record exists, you should check the second value from the graph access operation.

Immutable string-an error occurs if you want to update a separate character in a string variable through the index operator, because the string is a read-only byte. The correct way to do this is to use a single-byte slice instead of converting it to a string type.

String and byte conversion-when you convert a string to a byte slice, you get a complete copy of the raw data. Go provides a pair of optimized conversion operations for [] byte to string,string to [] byte to prevent additional allocation.

String and index operators-the index operators in a string return byte values instead of characters.

Strings are not always UTF8 text-- strings are not limited to UTF8 text. They can contain any byte. Is UTF8 text only when string constants are used.

String length-the RuneCountInString () function should be used in Go to get the string length value instead of the len () function.

Commas are missing in multiline slices, arrays, and graph definitions.

Log.Fatal and log.Panic: log libraries provide different log levels.

Preset data structure operations are not synchronized.

The string iteration value in range.

Use for range for graph iteration.

Note for Switch statements: the case blocks of switch statements are partitioned by default.

Increment and decrement: increment / subtraction of iTunes + or + + I is not supported in Go.

Bitwise is not an operator: many languages use ~ as a unary logical non-operator, while Go uses ^.

Operator precedence difference.

The exported domain is not encoded.

The Go thread was not destroyed when the program ended.

A buffer channel is sent back when the target receiver is ready.

The problem with closed channel transmission.

Use the "nil" channel.

A method with a value receiver cannot be used to change the original value.

What intermediate developers should pay attention to:

Close the HTTP response.

Close the HTTP connection.

Comparison of structures, arrays, slices and graphs.

You can use the = = operator to compare different structure variables.

Recover from the exception.

You can use recover () to catch / intercept exceptions.

Update and reference the item values of slices, arrays, and figure "range".

The "hidden" data of the slice.

Error in slicing data.

Slices of "stale".

Type declarations and methods.

Termination of for switch and for select code blocks.

Iteration variables and terminations in the for statement.

Delayed function call parameter evaluation.

Delay the execution of the function call.

The judgment of the wrong type.

Disconnected go threads and resource leaks.

What senior developers should pay attention to:

Use the pointer receiving method for the value instance.

Update the graph range.

"nil" interface and "nil" interface values.

Stack variables.

GOMAXPROCS, concurrency, parallelization.

Pre-emptive dispatch.

Take "curly braces cannot be separated into lines" as an example:

Erroneous writing:

[js] view plaincopy

Package main

Import "fmt"

Func main ()

{/ / error, can't have the opening brace on a separate line

Fmt.Println ("hello there!")

}

Compilation error prompt:

/ tmp/sandbox826898458/main.go:6: syntax error: unexpected semicolon or newline before {

Write it correctly:

[js] view plaincopy

Package main

Import "fmt"

Func main () {

Fmt.Println ("works!")

}

These are all the contents of this article entitled "what are the common pitfalls in Go language development?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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

Database

Wechat

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

12
Report