Source file src/unique/example_test.go

     1  // Copyright 2026 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package unique_test
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"unique"
    11  )
    12  
    13  func ExampleMake() {
    14  	type user struct {
    15  		Name string
    16  		Age  int
    17  	}
    18  
    19  	// Handles for equal values are equal, even when the values were
    20  	// constructed independently.
    21  	alice1 := user{Name: "Alice", Age: 30}
    22  	alice2 := user{Name: "Alice", Age: 30}
    23  	bob := user{Name: "Bob", Age: 30}
    24  
    25  	aliceHandle1 := unique.Make(alice1)
    26  	aliceHandle2 := unique.Make(alice2)
    27  	bobHandle := unique.Make(bob)
    28  
    29  	// The original values can be discarded, while the interned value remains
    30  	// available through the handle.
    31  	alice1 = user{}
    32  	alice2 = user{}
    33  	bob = user{}
    34  
    35  	fmt.Println(aliceHandle1 == aliceHandle2)
    36  	fmt.Println(aliceHandle1 == bobHandle)
    37  	fmt.Println(aliceHandle1.Value())
    38  
    39  	// Output:
    40  	// true
    41  	// false
    42  	// {Alice 30}
    43  }
    44  

View as plain text