xiaokang lin
xiaokang lin

Reputation: 183

Declared but not used

I am new to Golang. I am confused why I got the error that partial is not used in the code below?

// Using var keyword
var partial string

for i, request := range requestVec {
    if i == (len(requestVec)-1) && !strings.Contains(request, "\r\n\r\n") {
        partial = request
        break
    }
}

I assign request to partial in the if statement.

Upvotes: 0

Views: 2781

Answers (1)

Jem Tucker
Jem Tucker

Reputation: 1163

It is a compiler error in Go to declare a variable without using it.

In the code in you're example the variable partial is never used, only assigned, so is seen as unused. If you added additional code that read the variable then the error would be resolved. For example

var partial string

for i, request := range requestVec {
    if i == (len(requestVec)-1) && !strings.Contains(request, "\r\n\r\n") {
        partial = request
        break
    }
}

fmt.Println(partial) // We are now using `partial`

Upvotes: 1

Related Questions