Welcome To Golang By Example

Create Slice or Array of Floats in Go (Golang)

Overview

It is possible to create a slice or array of float data type in Golang as well. In fact, a slice or array can be created of any data type in Go. This tutorial contains simple examples to create a slice or array of float data type in golang.

Just to add here that in golang array is of fixed size and slice can have variable size. More details here

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

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

Slice of Float

package main

import "fmt"

func main() {

	//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)
	}
}

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

We have two ways of creating a slice of float. 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)

In the second way, we use make command to create a slice of floats

floats_second := make([]float64, 3)
floats_second[0] = 3.3
floats_second[1] = 2.2
floats_second[2] = 1.1

Array of Floats

package main

import "fmt"

func main() {

	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)
	}
}

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

We have two ways of creating an array. The first way is

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

In second way we directly initialize the array with created floats

floats_second := [3]float64{
	3.3,
	2.2,
	1.1,
}

Check out our Golang Advanced Tutorial. The tutorials in this series are elaborative and we have tried to cover all concepts with examples. This tutorial is for those who are looking to gain expertise and a solid understanding of golang – Golang Advance Tutorial

Also if you are interested in understanding how all design patterns can be implemented in Golang. If yes, then this post is for you –All Design Patterns Golang