You're creating new copies of the values and modifying the copies, rather
than storing a reference and then modifying the original data through it.
You'd use *string and *bool there to have both change.
This would be somewhat tedious and involve a good amount of type casting
though, if you were to keep doing it with interfaces like this. It could
well be that you'd be better served by avoiding them in this instance. But
if you must, then learn to enjoy type switches.

On Sat, May 24, 2025, 05:17 'jlfo...@berkeley.edu' via golang-nuts <
golang-nuts@googlegroups.com> wrote:

> I'm trying to write a program (see below) that passes a slice of structs
> to a function. One of the struct fields is an interface{} that sometimes
> will hold a boolean value and other times will hold a string value. To do
> this, I put either a bool or a string variable in the field.
>
> What I want to happen is for the local variable to be assigned a value.
> But, what's happening instead is only the struct field is assigned the
> value.
>
> Here's the program: (also at https://go.dev/play/p/7y5COCLU5EP)
>
> package main
>
> import (
> "fmt"
> )
>
> type i_t struct {
> arg interface{}
> }
>
> func main() {
>
> var help bool = false
> var fish string = "init"
>
> var i = []i_t{{help}}
> var t = []i_t{{fish}}
>
> fmt.Printf("before: help = %t\tstruct = %t\n", help, i)
> change_bool1(i)
> fmt.Printf("after: help = %t\tstruct = %t\n", help, i)
>
> fmt.Println()
>
> fmt.Printf("before: fish = %s\tstruct = %s\n", fish, t)
> change_string1(t)
> fmt.Printf("after: fish = %s\tstruct = %s\n", fish, t)
>
> }
>
> func change_bool1(a []i_t) {
>
> a[0].arg = true
> }
>
> func change_string1(a []i_t) {
>
> a[0].arg = "fish"
> }
>
> It generates the following output:
>
> before: help = false    struct = [{false}]
> after: help = false     struct = [{true}]
>
> before: fish = init     struct = [{init}]
> after: fish = init      struct = [{fish}]
>
> You can see that the values of the variables aren't changing but the
> values of the
> struct fields are. Is there some way for both to change?
>
> Cordially,
> Jon Forrest
>
>
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion visit
> https://groups.google.com/d/msgid/golang-nuts/bd06269a-7b6d-442a-a3f2-9d4f0020ac90n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/bd06269a-7b6d-442a-a3f2-9d4f0020ac90n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/CADgY6e93w_GSPEGkG9U3Kvkzv22-JHmX5%2BYM4f%3D_nFz6_NyntQ%40mail.gmail.com.

Reply via email to