Last active
July 26, 2022 17:24
-
-
Save nilsmagnus/ade521a230730012c82e to your computer and use it in GitHub Desktop.
Connect to redis with golang on heroku
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
}) | |
} |
Yup, that was helpful! You're also missing the net/url
import :)
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
Hey! I just found this, thanks for sharing it -- very helpful. I do see that
herokuURL
is undefined. It looks like putting avar herokuURL = ""
(e.g. after line 10) or just replacingherokuURL
withresolvedURL
would work