// This example demonstrates how copy-on-replace can be implemented
// for "immutable" datastructures.
This example demonstrates how copy-on-replace can be implemented
for "immutable" datastructures.
// This example demonstrates how copy - on - replace can be implemented // for " immutable " datastructures .
func Example_replace() {
	data, _ := demo.NewContainer(true)
	count := 0
	data2, changed, err := data.WalkTarget(
		func(ctx demo.TargetContext, x demo.Target) demo.TargetDecision {
			switch t := x.(type) {
			case *demo.ByRefType:
				count++
				cp := *t
				cp.Val = fmt.Sprintf("ByRef %d", count)
				return ctx.Skip().Replace(&cp)
			case *demo.ByValType:
				count++
				cp := *t
				cp.Val = fmt.Sprintf("ByVal %d", count)
				return ctx.Skip().Replace(&cp)
			default:
				return ctx.Continue()
			}
		})
	if err != nil {
		panic(err)
	}
	fmt.Printf("Changed: %v\n", changed)
	fmt.Printf("data != data2: %v", data != data2)
}
func Example_replace ( ) { data , _ := demo . NewContainer ( true ) count := 0 data2 , changed , err := data . WalkTarget ( func ( ctx demo . TargetContext , x demo . Target ) demo . TargetDecision { switch t := x . ( type ) { case * demo . ByRefType : count ++ cp := * t cp . Val = fmt . Sprintf ( " " , count ) return ctx . Skip ( ) . Replace ( & cp ) case * demo . ByValType : count ++ cp := * t cp . Val = fmt . Sprintf ( " " , count ) return ctx . Skip ( ) . Replace ( & cp ) default : return ctx . Continue ( ) } } ) if err != nil { panic ( err ) } fmt . Printf ( " \n " , changed ) fmt . Printf ( " " , data != data2 ) }
This example demonstrates how copy-on-replace can be implemented
for "immutable" datastructures.
This example demonstrates how copy - on - replace can be implemented for " immutable " datastructures .
//Output: //Changed: true //data != data2: true