Data Types

In the Go programming language, a "type" refers to the category of value that a variable or expression can hold. Some examples of built-in types in Go include:

  • int: a signed integer type, capable of holding values from -2^63 to 2^63-1

  • float64: a 64-bit floating-point type, capable of representing decimal values with a high degree of precision

  • string: a sequence of Unicode characters

  • bool: a Boolean type that can hold the values true or false

You can also create your own custom types by using the type keyword. Here's an example of creating a custom type "Myint" which is an int type

type MyInt int

Here's an example of creating a struct type person which has three fields name, age, and address

type person struct {
    name string
    age int
    address string
}

You can also create a custom type by using the alias keyword. Here's an example of creating a custom type "MyString" which is an alias of the string type.

type MyString = string

You can use these types just like the built-in types. For example, you can create a variable of type MyInt and assign it a value:

var myAge MyInt = 30

Last updated