# Dgraph database handler in Go

**URL:** <https://discuss.dgraph.io/t/dgraph-database-handler-in-go/2634>\
**Category:** Dgraph\
**Created:** [May 24, 2018, 6:20am UTC](https://discuss.dgraph.io/t/dgraph-database-handler-in-go/2634 "2018-05-24T06:20:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jack](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@jack](https://discuss.dgraph.io/u/jack)\
**Post date:** [May 24, 2018, 6:20am UTC](https://discuss.dgraph.io/t/dgraph-database-handler-in-go/2634/1 "2018-05-24T06:20:18Z")

</div>

Hello, I was hoping someone more experienced could comment on my Dgraph database handler, whether it could be improved in any way, and whether it’s a bad idea to just keep the connection open all the time? Here’s the code:

```auto
package db

import (
	"github.com/dgraph-io/dgo"
	"github.com/dgraph-io/dgo/protos/api"
	"google.golang.org/grpc"
)

type DgraphDB struct {
	Client *dgo.Dgraph
	Conn *grpc.ClientConn
}

var dgraphDB *dgo.Dgraph

func init() {
	configureDgraphDB()
}

func configureDgraphDB() {
	conn, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
	if err != nil {
		panic(err)
	}
	dgraphDB = dgo.NewDgraphClient(api.NewDgraphClient(conn))
}

func DgraphClient() *dgo.Dgraph {
	if dgraphDB == nil {
		doOnce.Do(configureDgraphDB)
	}
	return dgraphDB
}

```

And then I will be able to call the Dgraph client with:

```auto
db.DgraphClient()..Alter(context.Background(), &api.Operation{
   Schema: `
	username: string @index(term) .
	password: password .
	email: string @index(hash) @upsert .
	token: string .
   `,
})

```

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [May 24, 2018, 3:07pm UTC](https://discuss.dgraph.io/t/dgraph-database-handler-in-go/2634/2 "2018-05-24T15:07:42Z")

</div>

Are u using Dgo?  
I can not particularly help you with Golang (cus I do not have experience with it). However, Dgraph recently launched a separate client for Go.

> **[GitHub - dgraph-io/dgo: Official Dgraph Go client](https://github.com/dgraph-io/dgo)**
>
> Official Dgraph Go client. Contribute to dgraph-io/dgo development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![jack](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@jack](https://discuss.dgraph.io/u/jack)\
**Post date:** [May 24, 2018, 4:23pm UTC](https://discuss.dgraph.io/t/dgraph-database-handler-in-go/2634/3 "2018-05-24T16:23:46Z")

</div>

Yeah, I’m using the Dgo client, but I’m not sure if it’s bad that I keep the connection open 24/7, or if there’s a smarter way to initialize a Dgraph client to do all the communication with.

---

<div class="post-metadata">

**Author:** ![MichelDiz](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/micheldiz/32/11873_2.png) [@MichelDiz](https://discuss.dgraph.io/u/MichelDiz)\
**Post date:** [May 24, 2018, 5:27pm UTC](https://discuss.dgraph.io/t/dgraph-database-handler-in-go/2634/4 "2018-05-24T17:27:08Z")

</div>

Particularly I usually dig up test codes and other examples on several clients I have used (not just Dgraph) to understand the standards they generally use.

About continuous connection. Are you using it in an internal application? if so I do not see problem But for external clients I find it interesting to cut down unused connections. Create a pattern to disconnect and reconnect.
