An overview of the

You can also create a fragment or array of floating-point data types in Golang. In fact, fragments or arrays of any data type can be created in Go. This tutorial includes simple examples of creating shards or arrays of floating-point data types in Golang.

It should be added here that in Golang, arrays are fixed size, while fragments can have variable size. More details here

Array – https://golangbyexample.com/understanding-array-golang-complete-guide/

Slice – https://golangbyexample.com/slice-in-golang/

A fragment of a floating point number

package main

import "fmt"

func main(a) {

	//First Way
	var floats_first []float64
	floats_first = append(floats_first, 1.1)
	floats_first = append(floats_first, 2.2)
	floats_first = append(floats_first, 3.3)

	fmt.Println("Output for First slice of floats")
	for _, c := range floats_first {
		fmt.Println(c)
	}

	//Second Way
	floats_second := make([]float64.3)
	floats_second[0] = 3.3
	floats_second[1] = 2.2
	floats_second[2] = 1.1

	fmt.Println("\nOutput for Second slice of floats")
	for _, c := range floats_second {
		fmt.Println(c)
	}
}
Copy the code

The output

Output for First slice of floats
1.1
2.2
3.3

Output for Second slice of floats
3.3
2.2
1.1
Copy the code

There are two ways to create a fragment of a floating point number. The first way is

var floats_first []float64
floats_first = append(floats_first, 1.1)
floats_first = append(floats_first, 2.2)
floats_first = append(floats_first, 3.3)
Copy the code

Second, we use the make command to create a slice of a floating point number

floats_second := make([]float64.3)
floats_second[0] = 3.3
floats_second[1] = 2.2
floats_second[2] = 1.1
Copy the code

An array of floating point numbers

package main

import "fmt"

func main(a) {

	var floats_first [3]float64

	floats_first[0] = 1.1
	floats_first[1] = 2.2
	floats_first[2] = 3.3

	fmt.Println("Output for First Array of floats")
	for _, c := range floats_first {
		fmt.Println(c)
	}

	floats_second := [3]float64{
		3.3.2.2.1.1,
	}

	fmt.Println("\nOutput for Second Array of floats")
	for _, c := range floats_second {
		fmt.Println(c)
	}
}
Copy the code

The output

Output for First Array of floats
1.1
2.2
3.3

Output for Second Array of floats
3.3
2.2
1.1
Copy the code

There are two ways to create arrays. The first way is

var floats_first [3]float64
floats_first[0] = 1.1
floats_first[1] = 2.2
floats_first[2] = 3.3
Copy the code

In the second way, we initialize the array directly with the floating-point number we create

floats_second := [3]float64{
	3.3.2.2.1.1,}Copy the code

Take a look at our advanced Golang tutorial. This series of tutorials is carefully designed and we try to cover all the concepts with examples. This tutorial is for those who want to gain professional knowledge and a solid understanding of Golang – Golang Advanced Tutorial

If you are interested in learning how to implement all design patterns in Golang. If so, then this article is for you — All Design Patterns Golang

The postCreate Slice or Array of Floats in Go (Golang)appeared first onWelcome To Golang By Example.