HomeAuthorsContact
GO and the basics types

GO and the basics types

By Frédéric Schmidt
May 21, 2020
2 min read

Introduction

Ligne 2

The GO language is part of languages with static and strongly typed typing. So a variable is assigned a type from the start and cannot change it in time.

During compilation, the compiler will perform checks on the respect of the types on these.

The GO language has several type families as in other languages.

The GO Language has several entries for declaring variables.

The benefits

  • Detection of typographical errors during compilation and not during execution;
  • Most compilers will perform optimizations depending on the type;
  • The compiler can remove the knowledge of the type in the code thus a space gain, contrary to dynamic typing or we must know the type at any time.

Common types

Here is a presentation of the types that will be frequently manipulated through developments in GO language..

Boolean

What is a boolean

This type represents true/false logic.

Boolean Keyword

bool

Values

  • True
  • False

Playground

Example in action

package main

import "fmt"

func main() {

 var hasValided bool = false
 hasPublished := true

 fmt.Printf("%v - %T \n", hasValided, hasValided)
 fmt.Printf("%v - %T \n", hasPublished, hasPublished)
}

Character strings

What is a string

This type allows to represent string in UNICODE and immutable format.

Keyword for a string

string

String values

the string in quotation marks.

Playground

Example in action

package main

import "fmt"

func main() {

var message string = "Hello and Welcome to you dear"
name := "John Doe"

fmt.Printf("%s %s \n"", message, name)
fmt.Printf("Message variable is : len(%d) - type(%T) \n"", len(message), message)
fmt.Printf("The first letter for the name is %c\n"", name[0])
}

Integers

What is an integer

Allows to represent all the numbers said integer, as for example: 10, 20, 1920, 2020. On a certain number of bytes.

Keyword for integers

int

Values for integers

Signed or not signed values on 32 or 64 bits.

Playground

Example in action

package main

import "fmt"

func main() {

valeur1, valeur2 := 100, 150

result := valeur1 + valeur2

fmt.Printf("%d + %d = %d\n", valeur1, valeur2, result)
fmt.Printf("valeur1 variable is type(%T) \n"", valeur1)
}

Bytes

What is a byte

Allows to store values between 0.. 256. Byte corresponds to type uint8. So has an unsigned 8-bit integer.

Key word for bytes

byte

Value of one byte

Unsigned values from 0 to 256.

Playground

Example in action

package main

import "fmt"

func main() {

var message string = "Hello and Welcome to you dear"

var firstLettre byte = message[0]

fmt.Printf("%s\n", message)
fmt.Printf("Message variable is : len(%d) - type(%T) \n", len(message), message)
fmt.Printf("The first letter for the name is %c with the value of %d of type %T\n", firstLettre, firstLettre, firstLettre)
}

Floating

What is a floating

Stores a floating point number according to IEEE-754.

Keyword for floats

float32 (simple precision)
float64 (double precision)

Values of a float

Decimal values on 32 or 64 precision bits.

IEEE-754

IEEE Standard for Floating-Point Arithmetic (IEEE 754)

Playground

Example in action

package main

import (
 "fmt"
 "math"
)

func main() {

 var valeur float32 = 1.25662
 valeurOfPi := math.Pi

 fmt.Printf("%f %T\n"", valeur, valeur)
 fmt.Printf("%f %T %T\n"", valeurOfPi, valeurOfPi, math.Pi)
}

Comprehensive list of basic types

There are also these common types of uses. The following types: the signed integers

Definition

Allows to represent all the numbers said integer with the representation of negatives and positives

Keyword

int8 int16
int32
int64

Values

values signed according to the value of the number to be represented. For example for int8 will be values from -128 to 127.

unsigned integers

Definition

Allows to represent all the numbers said integer with the representation of positives only

Keyword

uint8
uint16
uint32
uint64

Values

values signed according to the value of the number to be represented. For example for int8 will be values from 0 to 255.


Tags

#ProgramingLanguages#go#goland#types
Frédéric Schmidt

Frédéric Schmidt

Software Architect

I'm Software Developer & Architect. I'm more than 20 years experiences in differents IT service companies and software editors. I like studies some new technologies and share this passion altroughts my technical blog.

Expertise

Architecture
Development
Modelisation
Technical writer

Social Media

githublinkedin

Related Posts

GO and Packages
Programming Languages
GO and Packages
June 09, 2020
1 min
© 2022, All Rights Reserved.

Quick Links

Contact Us

About me

Social Media