go-tutorial

Go Data Types

Data types in the Go programming language relate to a large framework for declaring variables and functions of various sorts. The type of a variable dictates how much storage space it takes up and how the stored bit pattern is interpreted.

The kinds in Go are divided into the following categories:

  1. Boolean types: They are boolean types with the following two predefined constants: true (a) false (b)
  2. Numeric types: They are arithmetic types that represent either integer or floating point values throughout the application.
  3. String types: A string type is a collection of string values. A series of bytes is its value. Strings are immutable types, which means they can’t be changed after they’ve been formed. String is the predefined string type.
  4. Derived types: Pointer types, Array types, Structure types, Union types, Slice types, Interface types, Map types, Channel Types and Function types are among them.

1. Integer Data Type

Whole numbers with zero, positive, and negative values but no decimal values are known as integers. For instance, 0 5 -1340.

The int keyword is widely used to declare integer numbers.

Go

var id int id is an integer variable in this case.

Multiple variables can be declared in the same line.

Go

var id, age int

2. Data Type Float

The float data type is used to store decimal values. For instance, 6.7, -34.2.

Keywords used: float32, float64

Here’s an illustration:

Go

var salary float64

In Go programming, there are two types of floating-point data.

  • float32 – 32 bits (4 bytes)
  • float64 – 64 bits (8 bytes)

3. String Data Type

A succession of characters is known as a string. “Hello,” “Hey there,” and so on.

Keyword: string

Here’s an illustration:

Go

var message string

To make strings in Go, we utilise double quotes or backticks

Go

<!-- wp:paragraph -->
<p>var message string = "Hello World "</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p> var message string = `Hello World`</p>
<!-- /wp:paragraph -->

4. Boolean Data Type

True or false are the two possible values for the boolean data type.

Keyword: bool

Go

var isValid bool

Example

This example shows some of the different data types in Go:

package main
import ("fmt")

func main() {
  var a bool = true     // Boolean
  var b int = 2         // Integer
  var c float32 = 9.44  // Floating point number
  var d string = "Hi! Learn Code Zone"  // String

  fmt.Println("Boolean: ", a)
  fmt.Println("Integer: ", b)
  fmt.Println("Float:   ", c)
  fmt.Println("String:  ", d)
}

OUTPUT

Boolean:  true
Integer:  2
Float:    9.44
String:  Hi! Learn Code Zone

RECOMMENDED ARTICLES





Leave a Reply

Your email address will not be published. Required fields are marked *