优化GetData
1. 去掉重复的ToLower 2. getData内部统一ToLower 3.调整getData中对空字符串判断位置 ==== 4. 待确定:在getData中是否有必要进行lock操作
Showing
1 changed file
with
9 additions
and
9 deletions
| ... | @@ -147,7 +147,7 @@ type IniConfigContainer struct { | ... | @@ -147,7 +147,7 @@ type IniConfigContainer struct { |
| 147 | 147 | ||
| 148 | // Bool returns the boolean value for a given key. | 148 | // Bool returns the boolean value for a given key. |
| 149 | func (c *IniConfigContainer) Bool(key string) (bool, error) { | 149 | func (c *IniConfigContainer) Bool(key string) (bool, error) { |
| 150 | return strconv.ParseBool(c.getdata(strings.ToLower(key))) | 150 | return strconv.ParseBool(c.getdata(key)) |
| 151 | } | 151 | } |
| 152 | 152 | ||
| 153 | // DefaultBool returns the boolean value for a given key. | 153 | // DefaultBool returns the boolean value for a given key. |
| ... | @@ -162,7 +162,7 @@ func (c *IniConfigContainer) DefaultBool(key string, defaultval bool) bool { | ... | @@ -162,7 +162,7 @@ func (c *IniConfigContainer) DefaultBool(key string, defaultval bool) bool { |
| 162 | 162 | ||
| 163 | // Int returns the integer value for a given key. | 163 | // Int returns the integer value for a given key. |
| 164 | func (c *IniConfigContainer) Int(key string) (int, error) { | 164 | func (c *IniConfigContainer) Int(key string) (int, error) { |
| 165 | return strconv.Atoi(c.getdata(strings.ToLower(key))) | 165 | return strconv.Atoi(c.getdata(key)) |
| 166 | } | 166 | } |
| 167 | 167 | ||
| 168 | // DefaultInt returns the integer value for a given key. | 168 | // DefaultInt returns the integer value for a given key. |
| ... | @@ -177,7 +177,7 @@ func (c *IniConfigContainer) DefaultInt(key string, defaultval int) int { | ... | @@ -177,7 +177,7 @@ func (c *IniConfigContainer) DefaultInt(key string, defaultval int) int { |
| 177 | 177 | ||
| 178 | // Int64 returns the int64 value for a given key. | 178 | // Int64 returns the int64 value for a given key. |
| 179 | func (c *IniConfigContainer) Int64(key string) (int64, error) { | 179 | func (c *IniConfigContainer) Int64(key string) (int64, error) { |
| 180 | return strconv.ParseInt(c.getdata(strings.ToLower(key)), 10, 64) | 180 | return strconv.ParseInt(c.getdata(key), 10, 64) |
| 181 | } | 181 | } |
| 182 | 182 | ||
| 183 | // DefaultInt64 returns the int64 value for a given key. | 183 | // DefaultInt64 returns the int64 value for a given key. |
| ... | @@ -192,7 +192,7 @@ func (c *IniConfigContainer) DefaultInt64(key string, defaultval int64) int64 { | ... | @@ -192,7 +192,7 @@ func (c *IniConfigContainer) DefaultInt64(key string, defaultval int64) int64 { |
| 192 | 192 | ||
| 193 | // Float returns the float value for a given key. | 193 | // Float returns the float value for a given key. |
| 194 | func (c *IniConfigContainer) Float(key string) (float64, error) { | 194 | func (c *IniConfigContainer) Float(key string) (float64, error) { |
| 195 | return strconv.ParseFloat(c.getdata(strings.ToLower(key)), 64) | 195 | return strconv.ParseFloat(c.getdata(key), 64) |
| 196 | } | 196 | } |
| 197 | 197 | ||
| 198 | // DefaultFloat returns the float64 value for a given key. | 198 | // DefaultFloat returns the float64 value for a given key. |
| ... | @@ -207,8 +207,7 @@ func (c *IniConfigContainer) DefaultFloat(key string, defaultval float64) float6 | ... | @@ -207,8 +207,7 @@ func (c *IniConfigContainer) DefaultFloat(key string, defaultval float64) float6 |
| 207 | 207 | ||
| 208 | // String returns the string value for a given key. | 208 | // String returns the string value for a given key. |
| 209 | func (c *IniConfigContainer) String(key string) string { | 209 | func (c *IniConfigContainer) String(key string) string { |
| 210 | key = strings.ToLower(key) | 210 | return c.getdata(key) |
| 211 | return c.getdata(strings.ToLower(key)) | ||
| 212 | } | 211 | } |
| 213 | 212 | ||
| 214 | // DefaultString returns the string value for a given key. | 213 | // DefaultString returns the string value for a given key. |
| ... | @@ -338,15 +337,16 @@ func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) { | ... | @@ -338,15 +337,16 @@ func (c *IniConfigContainer) DIY(key string) (v interface{}, err error) { |
| 338 | 337 | ||
| 339 | // section.key or key | 338 | // section.key or key |
| 340 | func (c *IniConfigContainer) getdata(key string) string { | 339 | func (c *IniConfigContainer) getdata(key string) string { |
| 341 | c.RLock() | ||
| 342 | defer c.RUnlock() | ||
| 343 | if len(key) == 0 { | 340 | if len(key) == 0 { |
| 344 | return "" | 341 | return "" |
| 345 | } | 342 | } |
| 343 | c.RLock() | ||
| 344 | defer c.RUnlock() | ||
| 345 | |||
| 346 | 346 | ||
| 347 | var ( | 347 | var ( |
| 348 | section, k string | 348 | section, k string |
| 349 | sectionKey []string = strings.Split(key, "::") | 349 | sectionKey []string = strings.Split(strings.ToLower(key), "::") |
| 350 | ) | 350 | ) |
| 351 | if len(sectionKey) >= 2 { | 351 | if len(sectionKey) >= 2 { |
| 352 | section = sectionKey[0] | 352 | section = sectionKey[0] | ... | ... |
-
Please register or sign in to post a comment