0

I am using etcd's wal package (https://godoc.org/github.com/coreos/etcd/wal) to do write-ahead logging. wal has go.uber.org/zap in its vendor packages. In wal's create function func Create(lg *zap.Logger, dirpath string, metadata []byte) (*WAL, error), I need to pass in zap.Logger.

I have tried to import go.uber.org/zap but go compiler complains "type mismatch" when I pass in zap.Logger.

package main 

import (
"github.com/coreos/etcd/wal"
)

func check(e error) {
    if e != nil {
        panic(e)
    }
}

func main() {

    metadata := []byte{}
    w, err := wal.Create(zap.NewExample(), "/tmp/hello", metadata)

    // err := w.Save(s, ents)


}

How should I use zap.Logger in my project?

lxjhk
  • 567
  • 2
  • 7
  • 13
  • 1
    Possible duplicate of [package's type cannot be used as the vendored package's type](https://stackoverflow.com/questions/38091816/packages-type-cannot-be-used-as-the-vendored-packages-type) – Jonathan Hall Jan 02 '19 at 07:03
  • Have you tried : add `go.uber.org/zap` in the vendored dependencies of your own project, and build from your project ? – LeGEC Jan 02 '19 at 09:21
  • @LeGEC, it gives a type mismatch. – ozn Jun 04 '20 at 23:43

1 Answers1

2

It seems like the package github.com/coreos/etcd/wal is not meant to be used outside of the etcd project. If you really need to use it, please, follow the steps below.

  1. Place the following code in the $GOPATH/src/yourpackage/main.go file.

    package main
    
    import (
        "fmt"
    
        "go.etcd.io/etcd/wal"
        "go.uber.org/zap"
    )
    
    func main() {
        metadata := []byte{}
        w, err := wal.Create(zap.NewExample(), "/tmp/hello", metadata)
        fmt.Println(w, err)
    }
    
  2. mkdir $GOPATH/src/yourpackage/vendor

  3. cp -r $GOPATH/src/go.etcd.io $GOPATH/src/yourpackage/vendor/
  4. mv $GOPATH/src/yourpackage/vendor/go.etcd.io/etcd/vendor/go.uber.org $GOPATH/src/yourpackage/vendor/
  5. go build yourpackage
Andrey Dyatlov
  • 1,628
  • 1
  • 10
  • 12
  • I dont think this works. Anyone can confirm? Even if it did, its not a very sustainable way of using this. – ozn Jun 04 '20 at 23:32