]> Cypherpunks repositories - gostls13.git/commitdiff
Add exvar.FuncInt for exporting indirect integer variables.
authorDavid Symonds <dsymonds@golang.org>
Sun, 24 May 2009 22:04:43 +0000 (15:04 -0700)
committerDavid Symonds <dsymonds@golang.org>
Sun, 24 May 2009 22:04:43 +0000 (15:04 -0700)
R=r
APPROVED=r
DELTA=21  (21 added, 0 deleted, 0 changed)
OCL=29320
CL=29338

src/lib/exvar/exvar.go
src/lib/exvar/exvar_test.go

index fea56833745bd4df0de032707327f9df054a7ee2..6473f7af62399278232edf21ce3f7d73a4dc54e6 100644 (file)
@@ -128,6 +128,14 @@ func (v *String) Set(value string) {
        v.s = value;
 }
 
+// IntFunc wraps a func() int64 to create a value that satisfies the Var interface.
+// The function will be called each time the Var is evaluated.
+type IntFunc func() int64;
+
+func (v IntFunc) String() string {
+       return strconv.Itoa64(v())
+}
+
 
 // All published variables.
 var vars map[string] Var = make(map[string] Var);
index 28fbf3cf2242a041aed3ecd3f160648b8a404a54..8b028bccb8b57f45422b3dd4668d070cf599b62b 100644 (file)
@@ -78,3 +78,16 @@ func TestMapCounter(t *testing.T) {
                t.Error("red = %v, want 3", x)
        }
 }
+
+func TestIntFunc(t *testing.T) {
+       x := int(4);
+       ix := IntFunc(func() int64 { return int64(x) });
+       if s := ix.String(); s != "4" {
+               t.Errorf("ix.String() = %v, want 4", s);
+       }
+
+       x++;
+       if s := ix.String(); s != "5" {
+               t.Errorf("ix.String() = %v, want 5", s);
+       }
+}