Go language AST Try

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

Go language has a lot of tools, goimports for the package automatic import or deletion, Golint used to check the source code does not conform to the go coding style, such as the full name, comments and so on. There are other tools such as Gorename, Guru and other tools. As tools they are developed using the Go language (see), which has one thing in common: reading the source code, parsing the source code, modifying or generating new code.

Briefly

Many programming languages/libraries/frameworks can generate code, for example, using rails, it's easy to get new project out and generate the code for the program, which we call boilerplate, or template, which is a common habit. Dynamic languages like Ruby can often generate code at run time, which we call Meta programming (metaprogramming), such as rails ' resources to generate restful router. Because it is dynamically generated at run time, As a result, you may experience exception and loss of performance.

The macro of a programming language like Elixir is a "front" step in Ruby's meta-programming, which generates code at compile time rather than run-time, with the benefit of generating a large amount of code that has little effect on performance. Like the Router view section of the Phoenix frame, a large number of functions are generated through the macro, using Beam's pattern matching mechanism for efficient routing. The macro of Elixir is written in the source code, while go can be separated.

The go language can also generate code (such as creating objects) through the reflect package as well as Ruby's runtime, but even more powerful is that it generates new code by reading the source and then modifying the source. We can write this process alone with a tool that can be applied to different projects.

Example

Stringer

Package Game//go:generate stringer-type=gamestatus//Note//with go:generate characters cannot have spaces//GameStatus indicates the race's status type GameStatus Intconst (unvalid gamestatus = iotavalidfailedvalidregisterstartrunningend)

Running go generate generates the Gamestatus_stirng.go file and implements the Stringer interface.

The same example also appears in the GRPC code, the generated string. As Rob Pike said:

Let the Mechine do the Work.source

Gen_columns

Many projects use the database, through the tag to specify the name of the field in the database, when writing SQL, and only through the string to represent the field name, so if a field name modification, it means that the SQL involved in this field is faced with modification, and we want to modify only one place.

There is a structure as a database table structure as follows:

Type User struct {    id int ' JSON: ' ID ' bson: ' id ' '    name string ' JSON: ' Name ' Bson: ' Name ' '}

When using the fields in this model for SQL queries, you typically use:

map[string]interface{}{    "id": 123456,}

As a query condition, if the field name changes, you have to modify the key value in this map.
If you can automatically generate a struct to represent these column name values, you only need to modify one place:

map[string]interface{}{    usercolumns.id:123456}

How to use

Gen_columns-tag= "Bson"-path= "./models/user.go"

Will generate a separate file with the following contents:

Package Modelstype _usercolumn struct {ID   stringname string}var usercolumns _usercolumnfunc init () {usercolumns.id = "id" usercolumns.name = "Name"}

Summarize
Gen_columns is the solution to the problem he has encountered in the project, the first version is done through reflect, a total of several steps required; With the AST you just need to add a go generate at compile time, and this command can basically be integrated in the build script, so you don't have to worry about code generation any more.
Let's use go to create more tools for generating code.

  

Other examples

    • IMPL specifies an interface that generates the required methods for the interface
    • Goa a set of DSLs for writing Web service
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.