Is there a general way to add metadata to a Go project?

Node.js has a file package.jsonthat contains several metadata in the module, such as its name, its version, its author (s), ...

How do you do this in Go?

For example, I would like to store the authors of the Go module somewhere with code. Is there a general way to do this?

+4
source share
1 answer

If the Go package has extensive documentation, by convention it should be placed in its own file doc.go.

Quote from a Godoc blog post: documentation go code :

, : doc.go, .

doc.

, "" , , :

const (
    Author      = "Joe Big"
    Version     = "0.1.2.3"
    ReleaseDate = "2015-11-08"
)

, godoc, , .

, godoc. , , :

const (
    Author      = "Joe Big"    // Package author: first name - last name
    Version     = "0.1.2.3"    // Package version: major.minor.maintenance.revision
    ReleaseDate = "2015-11-08" // Release date: year-month-day
)

, (, ReleaseDate ), , time.Parse(), / string time.Time. :

const ReleaseDateLayout = "2006-01-02" // Release date layout (for time.Parse())

, time.Time:

releaseDate, err := time.Parse(ReleaseDateLayout, ReleaseDate)

- , . :

// GetReleaseDate returns the release date as a value of time.Time.
func GetReleaseDate() time.Time {
    t, err := time.Parse(ReleaseDateLayout, ReleaseDate)
    if err != nil {
        panic(err)
    }
    return t
}

Version:

// GetVersion returns the parts of the Version as a slice:
//     {major, minor, maintenance, revision}
func GetVersion() []int {
    vs := []int{}
    for _, s := range strings.Split(Version, ".") {
        if v, err := strconv.Atoi(s); err != nil {
            panic(err)
        } else {
            vs = append(vs, v)
        }
    }
    return vs
}

, , . , , "" ( ), :

// This constant will not be in godoc and is unreachable from other packages
const codeReviewer = "Jane Small" // Last code reviewer: first name - last name
+7

Source: https://habr.com/ru/post/1614956/


All Articles