Next level of taking input


(brian cooper) #1

hey y’all I know I’m probably not asking the right questions as I search google so I need some human help. I’m trying to create a struct then assign a value to a var in the struct from the main. So I’ll pose a question then assign the response the appropriate var in the struct. What is that called? Seems like it’s a slice but I can’t find anything quite like it while I’m clicking around.

Here is a playground version of what I’m trying to do.


(Manxiaqu) #2

cat in your code is a struct not an actual instance, modify your code to https://play.golang.org/p/2CmZYJRwOgG, hope it helps.


(brian cooper) #3

worked like a champ! So you have to st the struct as an instance? Ok what does Ins do exactly?


(brian cooper) #4

oh and thanks


(Curtis Allyn Green) #5

Your

type cat struct{
	color string
	name string
}

is a type definition, it only tells the compiler what a cat struct consists of. It is not itself a cat object. In manxiaqu’s code the catIns is just the name of the variable that was chosen to be an instance of the cat type you defined. Thus the Ins part of the variable name just stands for Instance, but you could have named the variable pretty much anything.


(brian cooper) #6

so here is the problem. It won’t assign the string more than once. I did exactly like y’all showed me and it did work on the one but not on the second one. Was wondering why? The playground link is here. it will show you what I mean. I wanted it to save the values and print them out at the end.


(Manxiaqu) #7

I’m not exactly understand “work on the one but not the second one”, What’s the second one. do you mean a loop or something?


(brian cooper) #8

It is not assigning the string to the variable in the struct. At least not the second one.


(Manxiaqu) #9

I run your code locally, and here’s the result.
123123

what i see is that the input ‘a’ and ‘b’ have been assigned to c ins. what’s you excepted result if

Blockquote


(brian cooper) #10

mine does not. Hmmm, I’m using vscode as my editor and running it in the terminal. It takes the first input but skips taking the second one. wonder why? Glad it works for you. Wish it did for me.


(Manxiaqu) #11

it’s weird. the code is very simple and should work normal at different version or os. Any error message or logs(your terminal inputs/outpus) you can supply.