Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 9 gadiem
pirms 9 gadiem
pirms 9 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. env
  2. ===
  3. [![Build Status](https://travis-ci.org/goanywhere/env.svg?branch=master)](https://travis-ci.org/goanywhere/env) [![GoDoc](https://godoc.org/github.com/goanywhere/env?status.svg)](http://godoc.org/github.com/goanywhere/env)
  4. Ease of Accessing Environment Varaibles in Golang
  5. ### Installation
  6. ```shell
  7. $ go get -v github.com/goanywhere/env
  8. ```
  9. ### Usage
  10. ```shell
  11. PORT=9394
  12. SECRET_KEY=YOURSECRETKEY
  13. ```
  14. You can double/single quote string values:
  15. ```shell
  16. PRIVATE_KEY="-----BEGIN RSA PRIVATE KEY-----HkVN9…-----END DSA PRIVATE KEY-----"
  17. ```
  18. You can use `export` in front of each line just like your shell settings, so that you can `source` the file in your terminal directly:
  19. ```shell
  20. export USERNAME=account@goanywhere.io
  21. export PASSWORD=AccountPasswordGoesHere
  22. ```
  23. Export these values, you can then access them using env now.
  24. ``` go
  25. package main
  26. import (
  27. "github.com/goanywhere/env"
  28. )
  29. func main() {
  30. env.Int("PORT") // 9394
  31. env.String("SECRET_KEY") // YOURSECERTKEY
  32. .......
  33. }
  34. ```
  35. `env` also supports custom struct for you to access the reflected values.
  36. ``` go
  37. package main
  38. import (
  39. "fmt"
  40. "github.com/goanywhere/env"
  41. )
  42. type Spec struct {
  43. App string
  44. SecretKey string `env:"SECRET_KEY"`
  45. }
  46. func main() {
  47. var spec Spec
  48. env.Set("App", "myapplication")
  49. env.Set("SECRET_KEY", "wGv7ELIx8P8qsUit9OuWw2zwPEF0nXtvjIKZQOioAVuI5GnHSwBAeWZ6l4-SpIPT")
  50. env.Map(&spec)
  51. fmt.Printf("App: %s", spec.App) // output: "App: myapplication"
  52. fmt.Printf("Secret: %s", spec.SecretKey) // output: "Secret: wGv7ELIx8P8qsUit9OuWw2zwPEF0nXtvjIKZQOioAVuI5GnHSwBAeWZ6l4-SpIPT"
  53. }
  54. ```
  55. We also includes dotenv supports, simply add the application settings to file `.env` right under the root of your project:
  56. ``` text
  57. test1 = value1
  58. test2 = 'value2'
  59. test3 = "value3"
  60. export test4=value4
  61. ```
  62. ``` go
  63. package main
  64. import (
  65. "fmt"
  66. "github.com/goanywhere/env"
  67. )
  68. func main() {
  69. // Load '.env' from current working directory.
  70. env.Load()
  71. fmt.Printf("<test1: %s>", env.String("test1")) // output: "value1"
  72. fmt.Printf("<test2: %s>", env.String("test2")) // output: "value2"
  73. }
  74. ```
  75. ### NOTES
  76. Sensitive settings should **ONLY** be accessible on the machines that need access to them. **NEVER** commit them to a repository (even a private one) that is not needed by every development machine and server.