2

Consider package github.com/go-redis/redis/v8; its path ends with v8. So why, when we need to use function NewClient from that package, do we write redis.NewClient and not v8.NewClient?

package main

import (
    "github.com/go-redis/redis/v8"
)

func ExampleClient() {
    rdb := redis.NewClient(&redis.Options{
        Addr:     "localhost:6379",
        Password: "", // no password set
        DB:       0,  // use default DB
    })
    // do something with rdb
}
jub0bs
  • 60,866
  • 25
  • 183
  • 186
zhshou
  • 39
  • 2
  • 4
    There are several reasons why the package name is `redis` instead of `v8`: the name`redis` is more meaningful than `v8`; the name reduces the code that must be touched when upgrading to `v9`, the name avoids conflicts with other versioned packages. –  Mar 29 '22 at 15:41
  • 2
    The "v8" is more part of the module the package resides in. Often these import parts look like "github.com/foo/bar/v7/some/package" with github.com/foo/bar/v7 being the module (including the major version). It just happens that the redis package lives in the root of the module. – Volker Mar 29 '22 at 16:05
  • 3
    @jub0bs agreed. And many module maintainers avoid the "suggested" `v1` backward compatibility headache by just renaming the module path and go the [v2 and beyond](https://go.dev/blog/v2-go-modules) route (as in this case). – colm.anseo Mar 29 '22 at 16:15
  • 1
    The package name difference is not unique to modules, many packages use an import path where the last segment does not match the package name, `github.com/opentracing/opentracing-go` for example. See the documentation here for major version suffixes however: https://go.dev/ref/mod#major-version-suffixes – JimB Mar 29 '22 at 17:02

0 Answers0