Skip to content

proposal: Go 2: Annotations #38672

Closed
Closed
@joeysudoer

Description

@joeysudoer

Proposal

It is proposed to add Annotation feature in Go. The reasons are as follows:

  • There is no implementation of the annotation function in Go 1.x. A similar implementation is Struct Tags, but the use scenarios of struct tags are limited and cannot meet some more flexible requirements.

  • Through recent community surveys, it has been known that there are many Go developers who were also Java / Python developers before. The annotation feature in these languages ​​is very powerful. Based on the annotation function, you can flexibly implement some features, such as "dependency injection", which greatly Improve development efficiency.

  • There are some projects on GitHub that indirectly implement similar annotation functions through annotations, such as // @Test, but I think this way looks very weird and not elegant.

Syntax examples

Annotations can refer to the implementation specifications of annotation functions in most programming languages, using the @ symbol as the beginning, followed by the annotation name, and then providing parameters in parentheses after the name, such as

@Get('/user/:id')

The position of the annotation can be above the function, variable and other elements, such as

@Init
func SampleFunc(){

    @Config("sample.valueA")
    var valueA string
    ...

}

Commentary

I know this is a controversial proposal. I have communicated with many Go developers around me. They have shown a conservative attitude towards learning the language features of other languages, but I think that maintaining an open attitude will greatly benefit the development of Go.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions