Now consider functions. Let’s transcribe the declaration for main as it would read in Go, although the real main function in Go takes no arguments:

func main(argc int, argv []string) int
Languages outside the C family usually use a distinct type syntax in declarations. Although it’s a separate point, the name usually comes first, often followed by a colon. Thus our examples above become something like (in a fictional but illustrative language)

x: int
p: pointer to int
a: array[3] of int
One merit of this left-to-right style is how well it works as the types become more complex. Here’s a declaration of a function variable (analogous to a function pointer in C):

f func(func(int,int) int, int) int
These declarations are clear, if verbose - you just read them left to right. Go takes its cue from here, but in the interests of brevity it drops the colon and removes some of the keywords:

x int
p *int
a [3]int
Drop the parameter names and it’s just as clear - they’re always first so there’s no confusion.

func main(int, []string) int
The distinction between type and expression syntax makes it easy to write and invoke closures in Go:

sum := func(a, b int) int { return a+b } (3, 4)
Or if f returns a function:

f func(func(int,int) int, int) func(int, int) int
First, let’s talk about C syntax. C took an unusual and clever approach to declaration syntax. Instead of describing the types with special syntax, one writes an expression involving the item being declared, and states what type that expression will have. Thus

int x;
it’s not exactly clear that it’s a function pointer declaration at all. And what if the return type is a function pointer?

int (*(*fp)(int (*)(int, int), int))(int, int)
There’s one more point that needs to be made, though. Because type and declaration syntax are the same, it can be difficult to parse expressions with types in the middle. This is why, for instance, C casts always parenthesize the type, as in

(int)M_PI

Recommend

Go's Declaration Syntax Go syntax

Go's Declaration Syntax C syntax

Go Slices: usage and internals A possible “gotcha”

Go Slices: usage and internals Growing slices (the copy and append functions)

Go Slices: usage and internals Slice internals

Go Slices: usage and internals Slices

Go Slices: usage and internals Arrays

Go Codewalk: Generating arbitrary text: a Markov chain algorithm

Go

Weekly Snapshot History 2012-03-22 (Go 1 Release Candidate 2)

Weekly Snapshot History 2012-03-27 (Go 1)

Pre-Go 1 Release History r57 (released 2011/05/03) Language

Pre-Go 1 Release History r59 (released 2011/08/01) Packages

Go Release History

Go Canceling in-progress operations Canceling database operations after a timeout

Go Executing SQL statements that don't return data

Go Executing transactions Example

Go Querying for data Handling multiple result sets

Go Querying for data Handling nullable column values

Go Querying for data Querying for multiple rows

Go Querying for data Querying for a single row

Go Avoiding SQL injection risk

Go Using prepared statements How you use prepared statements

Go Opening a database handle Freeing resources

Go Opening a database handle Storing database credentials

Go Opening a database handle Confirming a connection

Go Opening a database handle Opening a database handle Opening with a Connector

Go Opening a database handle Opening a database handle Opening with a connection string

Go Opening a database handle Locating and importing a database driver

Go Call your code from another module

Go Compile and install the application

Go Return greetings for multiple people

Tutorial: Get started with Go Call code in an external package