Go language Notes-debugging is still weak, using GDB to do? Available panic and defer. Formatted code using GOFMT, it seems that my vim plugin is self-bringing

Source: Internet
Author: User
Tags stack trace

3.3 Debugger

Debugging is an essential part of the application development process, so it is very important to have a good debugger, unfortunately, Go in this aspect of development is not very perfect. Currently available debuggers are GDB, the latest version is built in the integrated development environment Liteide and Goclipse, but the debugger is not flexible and difficult to operate.

If you do not want to use the debugger, you can follow some of the following useful methods to achieve the purpose of basic debugging:

    1. Use the print statement to output the value of the relevant variable ( print / println and fmt.Print / fmt.Println / fmt.Printf ) in the appropriate location.

    2. fmt.Printfuse the following specifier in to print information about the variable:

      • %+vPrint full information about an instance that includes a field
      • %#vPrint full information for instances that include fields and qualified type names
      • %TPrint a full description of a type
    3. Use the Panic statement (Section 13.2) to get stack trace information (until panic, the list of all called functions).

    4. Use the keyword defer to track code execution (section 6.4).

Excerpt from: https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/03.3.md

In most Ides, the source Formatting tool is automatically called before each build gofmt and saves the formatted file. If the build succeeds, it will not output any information, and when a compile-time error occurs, it will indicate what error occurred in the specific line in the source code, such as: a declared and not used . In general, you can double-click the error message in the IDE to jump directly to the line where the error occurred.

3.5 Formatting Code

The Go development team did not want the go language to be like many other languages, always creating endless arguments for code style, wasting a lot of valuable development time, so they made a tool: go fmt ( gofmt ). This tool can format your source code into a style that conforms to the official uniform standard, and is a small refactoring on the grammatical style level. Following a unified code style is an unshakable iron rule in Go development, so you must use it gofmt to format your code before compiling or submitting a version management system.

Although there are some arguments to this approach, gofmt you no longer need to be a set of code styles and use the same rules for everyone. This not only enhances the readability of your code, but also allows you to quickly understand the meaning of its code as you take over an external Go project. In addition, most development tools also have this feature built in.

Go to the indentation level of the Code using tab or space is not mandatory, a tab can represent 4 or 8 spaces. In the actual development, 1 tab should represent 4 spaces, and in the case of itself, each tab represents 8 spaces. As for development tools, it is common to use tab instead of spaces.

Enter the code that gofmt –w program.go will format the source file at the command line and then overwrite the original content with the formatted code (if no parameters are -w printed only after the formatted result is not rewritten); gofmt -w *.go all Go source files are formatted and rewritten; gofmt map1 formatted and rewritten MAP1 All Go source files under the directory and its subdirectories.

gofmtYou can also -r implement simple refactoring of the code by adding the substitution rules enclosed in double quotation marks to the parameter, the format of the rule: <原始内容> -> <替换内容> .

Instance:

Gofmt-r ' (a)-a ' –w *.go

The code above will strip out the meaningless parentheses in the source file.

Gofmt-r ' A[n:len (a)], A[n:] ' –w *.go

The above code will remove the excess in the source file len(a) . (Translator Note: Understand the slices (slice) and then understand why this is superfluous)

Gofmt–r ' A.func1 (A, B), A.func2 (b,a) ' –w *.go

The above code swaps the parameters of the function in the source file that match the criteria.

If you would like gofmt more information about this, please visit this page: http://golang.org/cmd/gofmt/.

Excerpt from: https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/03.5.md

Go language Notes-debugging is still weak, using GDB to do? Available panic and defer. Formatted code using GOFMT, it seems that my vim plugin is self-bringing

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.