Skip to content

Instantly share code, notes, and snippets.

@nilsmagnus
Last active July 26, 2022 17:24
Show Gist options
  • Save nilsmagnus/ade521a230730012c82e to your computer and use it in GitHub Desktop.
Save nilsmagnus/ade521a230730012c82e to your computer and use it in GitHub Desktop.
Connect to redis with golang on heroku
import "gopkg.in/redis.v3"
var (
//Client for the database connection
client *redis.Client
)
func connect() {
var resolvedURL = os.Getenv("REDIS_URL")
var password = ""
if !strings.Contains(herokuURL, "localhost") {
parsedURL, _ := url.Parse(herokuURL)
password, _ = parsedURL.User.Password()
resolvedURL = parsedURL.Host
}
fmt.Printf("connecting to %s", herokuURL)
client = redis.NewClient(&redis.Options{
Addr: resolvedURL,
Password: password,
DB: 0, // use default DB
})
}
@maheshsundaram
Copy link

Hey! I just found this, thanks for sharing it -- very helpful. I do see that herokuURL is undefined. It looks like putting a var herokuURL = "" (e.g. after line 10) or just replacing herokuURL with resolvedURL would work

@christianrondeau
Copy link

Yup, that was helpful! You're also missing the net/url import :)

@herval
Copy link

herval commented Jul 26, 2022

welp thanks for posting this, just saved me some a considerable number of hours 👯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment