Saturday, May 21, 2011

Early syntax errors and other minor errors


Sometimes we know what works right, but we don’t know what works wrong. Let me make a list of a few errors that one could encounter so that you don’t waste much time figuring it out.

Unncessary Imports

Copy the code below into a file and execute it.

Full file: ErrProg1.go
package main

import "fmt"
import "os" //excessive - we are not using any function in this package

func main() {
 fmt.Println("Hello world")
}

Output:
prog.go:4: imported and not used: os

Go is particularly parsimonious when it comes to code - if you are not going to use something, don’t ask for it. Here, you have indicated that you want to import the os package but you haven’t used it anywhere. That’s not allowed. If you are not using it, remove it. If you remove the import os at line 4, this program will work.

Exact Names - case dependent


Full file: ErrProg2.go
package main

import "fmt"

func main() {
 fmt.println("Hello world")
}

Output:
prog.go:6: cannot refer to unexported name fmt.println
prog.go:6: undefined: fmt.println

Notice how we have written fmt.println and not fmt.Println. Go is case dependent, which means to say that when you use another’s name, use it exactly as it is defined. If the name is John, then only John works - not john, not joHn, and no other combination. So, in this case some of the others that are not allowed:

Invalid code
Package main
iMport "fmt"
import "Fmt"
Func main() {}
Fmt.Println
fmt.println

Separating lines with semicolons

If you are coming from a background in languages like C, C++, Java, Perl, etc. you will notice that Go (at least so far) has not required you to put semi colons at the end of the line. In Go, the new line character automatically indicates the end of the line. However, if you happen to put two statements in the same line, then you need to have a semicolon separating them. Let’s take a look at an example.

Full file: ErrProg3.go
package main

import "fmt"

func main() {
 fmt.Println("Hello world") fmt.Println("Hi again")
}

Output:
prog.go:6: syntax error: unexpected name, expecting semicolon or newline or }

Now you could make this work by putting the two Println statements on two separate lines, like so:

Partial file
func main() {
 fmt.Println("Hello world") 
            fmt.Println("Hi again")
}

But for the purpose of illustrating this example, try out this version.

Full file:
package main

import "fmt"

func main() {
 fmt.Println("Hello world"); fmt.Println("Hi again")
}

Output:
Hello world
Hi again

So, semi colons in Go can be used but are not compulsory on every line. But if you are going to have multiple statements in a line, you need to separate them with semi colons.

So this one is also valid and will get you the same output.
Full file: ErrProg4.go
package main;

import "fmt";

func main() {
 fmt.Println("Hello world"); fmt.Println("Hi again");
};

Output:
Hello world
Hi again

But watch out where you go with all those free semicolons.

Unnecessary semicolons


Let’s simplify that program, but go overboard with our semicolons. Try out this code now.

Full file
package main

import "fmt";;

func main() {
 fmt.Println("Hello world")
}

Output:
prog.go:3: empty top-level declaration

Again, Go is strictly frugal with its code. Here, alongside the import statement, there are two semicolons. Now the first one is acceptable - not necessary here, but acceptable. But two! Nope, that is where Go draws the line. The semicolon indicates the end of a statement, but there is no valid statement prior to the second semicolon. So remove the extra semicolon and all should be fine again.

Syntax and other things

The compiler demands that you follow proper syntax. There are a large possibility of syntax errors and it wouldn’t be a good idea to list them all. But I shall list a few. If you know these ones, most of the rest are just similar.

package 'main' //ERROR - no quotes for the package name: package main
package "main" //ERROR - no quotes for the package: package main

package main.x  //ERROR - packages names in go are just one expression.  So either package main or package x.
package main/x  //ERROR - packages names in go are just one expression.  So either package main or package x.

import 'fmt' //ERROR - needs double quotes "fmt"
import fmt //ERROR - needs double quotes "fmt"

func main { } //ERROR - functions have to be followed by parantheses: func main() {}

func main() [] //ERROR - where curly braces are required, only those are allowed.  They are used to contain blocks of code.  func main() {}

func main() { fmt.Println('hello world') } //ERROR - use double quotes for strings: func main() { fmt.Println("hello world") }

3 comments:

If you think others also will find these tutorials useful, kindly "+1" it above and mention the link in your own blogs, responses, and entries on the net so that others also may reach here. Thank you.

Note: Only a member of this blog may post a comment.