Getting 'undefined variable' error in my Golang code, need help debugging

kritishah 10 Reputation points
2023-09-11T05:45:41.6566667+00:00

Hello

I'm working on a Golang project, and I've encountered an issue that I can't seem to resolve. I'm getting an 'undefined variable' error, and I'm not sure why it's happening. Here's a simplified version of my code as below . When I run this code, I get an error saying undefined: anotherMessage. I've tried declaring anotherMessage before using it, but it doesn't seem to fix the issue. Can someone please explain what's causing this error and how I can resolve it? Any help would be greatly appreciated.

package mainimport "fmt"func main() { message := "Hello, world!" fmt.Println(message) fmt.Println(anotherMessage) // This line throws an error}

I have checked https://learn.microsoft.com/en-us/answers/support/Golang Certification for guidance .

Thank you

Developer technologies Visual Studio Debugging
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anna Xiu-MSFT 31,056 Reputation points Microsoft External Staff
    2023-09-11T08:14:08.8166667+00:00

    Hi @kritishah

    Welcome to Microsoft Q&A! 

    Are you using Visual Studio Code?

    The tag “Visual Studio Debugging” is related to Visual Studio IDE which is different from the Visual Studio Code.

    If you have any questions, please report it in dedicated Visual Studio Code forum or Go forum

    For your issue, I suppose the anotherMessage is not defined properly. Maybe you can check the following:

    User's image

    Sincerely,

    Anna


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.