Go: How to include an external library and exclude unnecessary dependencies



TL; DR: Code generator.



Imagine that you are developing some library that supports various storage drivers: Postgresql, Scylla, JSON, and some others.



Each of these drivers has external dependencies on other libraries. Postgresql - database / sql, github.com/lib/pq. Scylla - github.com/scylladb/gocql or standard github.com/gocql/gocql. JSON - github.com/mailru/easyjson or github.com/json-iterator/go, or encoding / json.



.



, : Postgres , , .



, .







, . : sql.go, cql.go, json.go.



, .





pkger .



pkger —



    files := make([]string, 0)

    if *cql {
        files = append(files, pkger.Include("/cql.go"))
    }

    if *json {
        files = append(files, pkger.Include("/json.go"))
    }

    if *sql {
        files = append(files, pkger.Include("/sql.go"))
    }




pkger -o <   >


, .



go generate



//go:generate pkger -o <   >


, .





— Go, .



    for i := range files {
        _, filename := filepath.Split(files[i])

        in, err := pkger.Open(files[i])
        if err != nil {
            panic(err)
        }

        src := []byte(fmt.Sprintf(genStr, strings.Join(os.Args[1:], " ")))

        reader := bufio.NewReader(in)

        for {
            line, err := reader.ReadBytes('\n')
            if strings.HasPrefix(string(line), "package ") {
                line = []byte(fmt.Sprintf("package %s\n", *pkgName))
            }

            src = append(src, line...)

            if err == io.EOF {
                break
            }

            if err != nil {
                panic(err)
            }
        }

        if err := ioutil.WriteFile(filename, src, 0644); err != nil {
            panic(err)
        }

        in.Close()
    }


“cmd/< >”. , .







go get github.com/< >/< >/<  >


And then starts the code generator in his project



<    > -sql


When you need to update your library - the user updates the code generator



go get -u github.com/< >/< >/<  >


And then it starts the generation again.



An example of this approach to using dependencies is I and crekerimplemented in the nan library : you can work with the library both in the usual mode, through import, and in the code generation version.




All Articles