One of my roommates pointed out that this shouldn't throw a warning or
error because I didn't assign a value to the variable, but it should be a
warning because I hide the scope of the original text variable with the
text variable inside the loop scope. The C# compiler throws a warning for a
very similar issue (level 2 compiler warning CS0108), and it would not
compile the code with that exact error (CS0128) that I created.
On Tuesday, January 30, 2018 at 6:05:57 PM UTC-6, Jacob Lancaster wrote:
>
> So, I'm new to Go, but I wanted to make a cli app that plays go fish with
> another client over a network. While I was testing the main loop that takes
> in the commands, I noticed that the program wouldn't exit when I entered
> the exit command. Here is my code before I found the issue.
>
> var text string //Instantiating the variable that would hold the command
> in a scope where the loop can check it
> for text != "exit\n" {
> fmt.Println("Enter command")
> text, err := reader.ReadString('\n') //This is where I realized the
> error was. I create another text variable that is in the scope of the loop.
> }
>
> I know this was a beginner mistake, and looking back at it, I feel stupid,
> but it got me thinking. Go gives a compiler error if you declare a variable
> and don't use it. In this instance I did use the outer scope variable text,
> but I never gave it a value in my code. Now, I don't know if there are
> warnings already in place that will tell you this. If there aren't,
> however, I think there should be at least a compiler warning. I was told to
> post something here before I open an issue. So really this is just me
> asking if a compiler warning for this would be a good idea, or is this just
> a beginner mistake that no one runs into if they know what they're doing.
>
>
>
>
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.