質問編集履歴

3

編集

2021/09/24 08:26

投稿

enokia
enokia

スコア23

test CHANGED
File without changes
test CHANGED
@@ -44,4 +44,98 @@
44
44
 
45
45
 
46
46
 
47
+ config.ini
48
+
49
+ ```
50
+
51
+ [web]
52
+
53
+ port = 8080
54
+
55
+ logfire = webapp.logfire
56
+
57
+
58
+
59
+ [db]
60
+
61
+ driver = sqllite3
62
+
63
+ name = webapp.sql
64
+
65
+
66
+
67
+ ```
68
+
69
+
70
+
71
+ config.go
72
+
73
+ ```
74
+
75
+ package config
76
+
77
+
78
+
79
+ import "log"
80
+
81
+
82
+
83
+ type ConfigList struct {
84
+
85
+ Port string
86
+
87
+ SQLDriver string
88
+
89
+ DbName string
90
+
91
+ LogFile string
92
+
93
+ }
94
+
95
+
96
+
97
+ var Config ConfigList
98
+
99
+
100
+
101
+ func init () {
102
+
103
+ LoadConfig()
104
+
105
+ }
106
+
107
+
108
+
109
+ func LoadConfig() {
110
+
111
+ cfg, err := ini.Load("config.ini")
112
+
113
+ if err != nil {
114
+
115
+ log.Fatalln()
116
+
117
+ }
118
+
119
+ Config = ConfigList{
120
+
121
+ Port: cfg.Section("web").Key("port").MustString("8080"),
122
+
123
+ SQLDriver: cfg.Section("db").Key("driver").String(),
124
+
125
+ DbName: cfg.Section("db").Key("name").String(),
126
+
127
+ LogFile: cfg.Section("web").Key("logfile").String(),
128
+
129
+ }
130
+
131
+ }
132
+
133
+
134
+
135
+ ```
136
+
137
+
138
+
139
+
140
+
47
141
  ![イメージ説明](5217451e622111074caf8b8ee4765e57.png)

2

変更

2021/09/24 08:26

投稿

enokia
enokia

スコア23

test CHANGED
File without changes
test CHANGED
@@ -28,6 +28,14 @@
28
28
 
29
29
  fmt.Println("テスト")
30
30
 
31
+ fmt.Println(config.Config.Port)
32
+
33
+ fmt.Println(config.Config.SQLDriver)
34
+
35
+ fmt.Println(config.Config.DbName)
36
+
37
+ fmt.Println(config.Config.LogFile)
38
+
31
39
  }
32
40
 
33
41
 

1

編集

2021/09/24 08:19

投稿

enokia
enokia

スコア23

test CHANGED
File without changes
test CHANGED
@@ -33,3 +33,7 @@
33
33
 
34
34
 
35
35
  ```
36
+
37
+
38
+
39
+ ![イメージ説明](5217451e622111074caf8b8ee4765e57.png)