0

I am trying to import github.com/dgraph-io/dgo and google.golang.org/grpc in my Go code. But when I save the file it gets removed. This is how I am trying :

import (
     "bytes"
     "encoding/json"
     "fmt"
     "io/ioutil"
     "net/http"
     
     "google.golang.org/grpc"
     "github.com/dgraph-io/dgo"
)

I tried to get github.com/dgraph-io/dgo package using command go get github.com/dgraph-io/dgo

Can anyone please help what is prerequisite to use given packages. My aim is to use Dgraph queries and Mutations in Go

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
rachana
  • 3,344
  • 7
  • 30
  • 49
  • 4
    You probably have `goimports` as part of your save hook and you are not using the packages in your code, so it just removes them. – Marc Aug 07 '20 at 10:47
  • 2
    Go does not allow unused imports: if you do not reference `dgo` in the file's code, that file won't compile. As @Marc suggested, if your IDE runs `goimports`—a popular tool to guess which packages need to be imported (by looking at the package references in the code)—upon saving the file, that would indeed explain the observed behaviour. – kostix Aug 07 '20 at 10:50
  • 2
    Still note that _some_ packages might indeed need to be imported even if they're not directly used—for side effectes of their initialization. These days this is considered to be an anti-pattern and bad practice but, say, drivers for `database/sql` has to behave this way. If _and only if_ `dgo` is indeed such a package (and you have to figure that out in its docs) you need to use `import _ "github.com/dgraph-io/dgo"`—that is import the package as the `_` identifier. But, again, please check the docs first. – kostix Aug 07 '20 at 10:53

0 Answers0