-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
118 lines (89 loc) · 3.04 KB
/
main.go
File metadata and controls
118 lines (89 loc) · 3.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"fmt"
"log"
"github.com/cloudresty/go-env"
)
func main() {
fmt.Println("=== Base64 Encoding/Decoding Example ===")
// 1. Basic base64 operations
fmt.Println("1. Basic base64 operations:")
originalData := "Hello, World! This is some data that will be base64 encoded."
// Store data as base64
err := env.SetB64("ENCODED_MESSAGE", originalData)
if err != nil {
log.Fatalf("Failed to set base64 data: %v", err)
}
fmt.Printf(" Original data: %s\n", originalData)
// Show the raw encoded value
rawEncoded := env.Get("ENCODED_MESSAGE")
fmt.Printf(" Raw base64 value: %s\n", rawEncoded)
// Decode the data
decoded, err := env.GetB64("ENCODED_MESSAGE")
if err != nil {
log.Fatalf("Failed to decode base64 data: %v", err)
}
fmt.Printf(" Decoded data: %s\n", decoded)
fmt.Printf(" ✓ Data matches: %t\n", originalData == decoded)
// 2. Handling binary-like data
fmt.Println("\n2. Handling binary-like data:")
binaryData := "This contains special chars: 🔒 🌟 ♠ ♥ ♦ ♣ \x00\x01\x02"
err = env.SetB64("BINARY_DATA", binaryData)
if err != nil {
log.Fatalf("Failed to set binary data: %v", err)
}
fmt.Printf(" Original binary data: %q\n", binaryData)
decodedBinary, err := env.GetB64("BINARY_DATA")
if err != nil {
log.Fatalf("Failed to decode binary data: %v", err)
}
fmt.Printf(" Decoded binary data: %q\n", decodedBinary)
fmt.Printf(" ✓ Binary data matches: %t\n", binaryData == decodedBinary)
// 3. Using with default values
fmt.Println("\n3. Using with default values:")
// Try to get a non-existent base64 variable with a default
defaultMessage := "This is the default message"
result, err := env.GetB64("NON_EXISTENT_B64", defaultMessage)
if err != nil {
log.Printf("Error getting with default: %v", err)
} else {
fmt.Printf(" Non-existent variable with default: %s\n", result)
}
// 4. Error handling - invalid base64
fmt.Println("\n4. Error handling:")
// Manually set an invalid base64 value
_ = env.Set("INVALID_B64", "This is not valid base64!")
_, err = env.GetB64("INVALID_B64")
if err != nil {
fmt.Printf(" ✓ Properly caught invalid base64 error: %v\n", err)
} else {
fmt.Printf(" ✗ Should have failed for invalid base64\n")
}
// 5. Practical example - storing configuration
fmt.Println("\n5. Practical example - storing JSON configuration:")
jsonConfig := `{
"database": {
"host": "localhost",
"port": 5432,
"ssl": true
},
"features": ["auth", "logging", "metrics"]
}`
err = env.SetB64("APP_CONFIG", jsonConfig)
if err != nil {
log.Fatalf("Failed to store JSON config: %v", err)
}
fmt.Println(" ✓ Stored JSON configuration as base64")
retrievedConfig, err := env.GetB64("APP_CONFIG")
if err != nil {
log.Fatalf("Failed to retrieve JSON config: %v", err)
}
fmt.Println(" ✓ Retrieved JSON configuration:")
fmt.Printf(" %s\n", retrievedConfig)
// Clean up
_ = env.Unset("ENCODED_MESSAGE")
_ = env.Unset("BINARY_DATA")
_ = env.Unset("INVALID_B64")
_ = env.Unset("APP_CONFIG")
fmt.Println("\n=== Base64 Example Complete ===")
}