A blog about programming, Web development, Open Source, Linux and DevOps.

Go - Type Declarations

Type declarations come in two forms: alias declarations and type definitions.

Alias Declaration

A type alias creates a new name for an existing type.

// Alias declaration syntax (An equal sign is used).
type Alias = ExistingType


// An alias for int64 named Timestamp.
// Timestamp and int64 are identical types.
type Timestamp = int64
// Declare multiple type aliases.
type (
nodeList = []*Node // nodeList and []*Node are identical types
Score = int // Score and int are identical types

Type Definition

A type definition creates a new, distinct type, it is different from any other type including the type it’s created from and may have methods associated with it.

// Type definition syntax.
type NewType ExistingType


// New type named Duration based on int64.
// Duration and int64 are different types.
type Duration int64
// Define multiple types.
// All the defined types are different from the types they're created from.
type (
TimeZone int
ByteSlice []byte
Point struct{ x, y float64 }