Dgraph database handler in Go

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:

package db

import (

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

var dgraphDB *dgo.Dgraph

func init() {

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

func DgraphClient() *dgo.Dgraph {
	if dgraphDB == nil {
	return dgraphDB

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

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

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.

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.

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.