Merge pull request #9 from uber-go/string_bug

Fix NewString disregarding the passed in string
This commit is contained in:
Prashant Varanasi
2016-07-18 11:25:57 -07:00
committed by GitHub
2 changed files with 7 additions and 3 deletions

View File

@@ -26,8 +26,12 @@ import "sync/atomic"
type String struct{ atomic.Value }
// NewString creates a String.
func NewString(s string) *String {
return &String{}
func NewString(str string) *String {
s := &String{}
if str != "" {
s.Store(str)
}
return s
}
// Load atomically loads the wrapped string.

View File

@@ -39,5 +39,5 @@ func TestString(t *testing.T) {
require.Equal(t, "abc", atom.Load(), "Unexpected value after Store")
atom = NewString("bcd")
require.Equal(t, "", atom.Load(), "Expected Load to return initialized value")
require.Equal(t, "bcd", atom.Load(), "Expected Load to return initialized value")
}