Go Data type

admin Avatar

GO:

Go is a statically typed programming language, which means that every variable and expression must have a specific type. In this blog post, we’ll explore the various data types available in Go and provide examples for each one.

Boolean

The boolean data type represents a logical value of either true or false. In Go, boolean values are represented using the bool type. Here’s an example:

var isTrue bool = true

var isFalse bool = false

Numeric

Go supports several numeric data types, including int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, and float64. Here are some examples:

var integer int = 42

var decimal float64 = 3.14

String

The string data type represents a sequence of characters. In Go, strings are represented using the string type. Here’s an example:

var message string = “Hello, world!”

Arrays

Arrays are a collection of values of the same type. In Go, arrays are represented using brackets and the type of the elements. Here’s an example:

var numbers [5]int = [5]int{1, 2, 3, 4, 5}

Slices

Slices are similar to arrays, but their length is flexible. In Go, slices are represented using brackets and the type of the elements. Here’s an example:

var letters []string = []string{“a”, “b”, “c”}

Structs

Structs are user-defined types that can contain multiple values of different types. In Go, structs are defined using the struct keyword. Here’s an example:

type Person struct {

    name string

    age int

Advertisements

}

var john Person = Person{“John”, 30}

Pointers

Pointers are variables that store memory addresses. In Go, pointers are represented using the * operator. Here’s an example:

var number int = 42

var pointer *int = &number

In conclusion, Go provides a range of data types to work with, including boolean, numeric, string, arrays, slices, structs, and pointers. By using these data types, developers can write clear and expressive code that is easy to read and maintain.

Tagged in :

admin Avatar

More Articles & Posts