Go Basic Syntax
Comments
Write comments using // ... or /* ... */. Comments are ignored during compilation.
// This is a comment.
/* This is also
a comment. */
Semicolons at the end of a line
Semicolons (;) can be written at the end of statements, but they are usually omitted. Use semicolons to write multiple statements on one line.
num = 123; str = "ABC";
Escape sequences
Strings and characters can contain these escape sequences:
\a bell (U+0007)
\b backspace (U+0008)
\t tab (U+0009)
\n newline (U+000A)
\v vertical tab (U+000B)
\f form feed (U+000C)
\r carriage return (U+000D)
\" double quote (U+0022)
\' single quote (U+0027)
\\ backslash (U+005C)
\x42 ASCII character (U+0000-U+00FF)
\u30A2 Unicode character (U+0000-U+FFFF)
\U0001F604 Unicode character (U+0000-U+10FFFF)
Keywords
break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return var
Imports
Use import to load a package.
import "fmt"
Import multiple packages in a group:
import (
"os"
"fmt"
)
Deferred execution
A defer statement runs immediately before the surrounding function returns. It is often used to ensure that resources are released.
func funcA() {
fp, err := os.Open("sample.txt")
if err != nil {
return
}
defer fp.Close()
for {
...
}
}