I've been writing a test program to assist in learning Go. While doing so, I've encountered a few instances where I thought the compiler should have detected an error. I've encountered another similar situation, so I thought I should ask why this situation is not treated as an error.
Example situation :
if oError = rwfile.WriteLines(asParams, sParamsFilename); oError != nil {
fmt.Printf("Error on write to file Params. Error = %s
", oError)
} else {
println("Params file write OK")
}
In the example above, whether or not the variable "oError" is declared, the compiler does not indicate an error. It also works if the line contains the following when the variable is not declared (as expected):
if oError := rwfile.WriteLines(asParams, sParamsFilename); oError != nil {
If I declare the variable "oError", then ":=" does not work (as expected).
"rwfile" is a package that I have written, and the function in question starts as follows:
func WriteLines(asBuff []string, sFilename string) error { // write text file
If I create an error with the write of the file, and use "=" without declaring the variable "oError", the program works correctly and detects the non-nil "oError" variable.
So, why is use of "=" in the above not treated as an error when oError is not declared a variable?
The Go version is go1.1.2 Windows/386.