Re: [go-nuts] Question About Interface Field in a Structure

2025-05-24 Thread Kurtis Rader
The problem starts with these two lines: > var i = []i_t{{help}} > var t = []i_t{{fish}} You are initializing the structs with the values stored in `help` and `fish`, not references to those variables. Thus, when you change the value stored in the struct it has no effect on the variables from whi

Re: [go-nuts] Question About Interface Field in a Structure

2025-05-24 Thread 'jlfo...@berkeley.edu' via golang-nuts
Bingo! This solves the problem. You win the prize! I hope this discussion helps other people facing similar problems. Jon -- 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

Re: [go-nuts] Question About Interface Field in a Structure

2025-05-24 Thread Mikk Margus
Sorry, I seem to have copy-pasted your Go Playground link instead of mine, assuming "share" would place it in my clipboard automatically. https://go.dev/play/p/8XajdwXDdqW This is what I meant to share. It outputs the following: ``` before: help = falsestruct = [{%!t(*bool=0xc1006d)}]

Re: [go-nuts] Question About Interface Field in a Structure

2025-05-24 Thread 'jlfo...@berkeley.edu' via golang-nuts
Thanks for your and Brian's replies. But, unless I'm missing something, neither solve the problem. I ran both of them in the Go Playground and they both produced the same incorrect result. The result I'm looking for would be: before: help = false struct = [{false}] after: help = true struct =

Re: [go-nuts] Question About Interface Field in a Structure

2025-05-24 Thread Mikk Margus
As far as I can tell, they're asking for a way for `var help`/`var fish` etc. to get updated alongside the attribute `i_t.arg` in the update methods. This example accomplishes this. https://go.dev/play/p/7y5COCLU5EP Do note that it crashes and burns if the pointer is not of the expected type, a

Re: [go-nuts] Question About Interface Field in a Structure

2025-05-24 Thread 'Brian Candler' via golang-nuts
Or you can use a setter method: https://go.dev/play/p/W9Cz2PO8NeK On Saturday, 24 May 2025 at 03:39:34 UTC+1 Def Ceb wrote: > 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

Re: [go-nuts] Question About Interface Field in a Structure

2025-05-23 Thread Def Ceb
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

[go-nuts] Question About Interface Field in a Structure

2025-05-23 Thread 'jlfo...@berkeley.edu' via golang-nuts
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