I just started to play with Go-lang and come across a Strange behavior of it Structs. I have a Struct A
and another Struct B
, in Struct B
one key defined as []A
the problem is when assigning the value of new instance type of B
as elements of A
it throw error despite the types are same. Any help will be greatly appreciated Below here I am pasting the minimal code which cause error
package main
import (
"fmt"
"math"
"github.com/shirou/gopsutil/disk"
"strconv"
)
func main() {
/************ disk details goes here ************/
diskPartitions, err := disk.Partitions(true)
dealwithErr(err)
fmt.Println(diskPartitions)
type PARTITIONLIST []PARTITION
var partitionsList PARTITIONLIST
for partitionIndex, partition := range diskPartitions {
partitionStat, err := disk.Usage(partition.Mountpoint)
dealwithErr(err)
var partitionDetails = PARTITION{
"PARTITION",
partitionIndex,
partition.Mountpoint,
"" + fmt.Sprint(partitionStat.Total) + " and " + bytesToSize(partitionStat.Total),
"" + fmt.Sprint(partitionStat.Used) + " and " + bytesToSize(partitionStat.Used),
"" + fmt.Sprint(partitionStat.Free) + " and " + bytesToSize(partitionStat.Free),
"" + fmt.Sprint(partitionStat.UsedPercent) + "and " + strconv.FormatFloat(partitionStat.UsedPercent, 'f', 2, 64),
}
partitionsList = append(partitionsList, partitionDetails)
}
//till here working fine
fmt.Println(partitionsList)
//THE BELOW TWO LINES ERROR IS THE ACTUAL ERROR I AM ASKING
var partitionDetails = PARTITIONS{
"partitions",
partitionsList
}
dealwithErr(err)
}
/************ all struct goes here ************/
type PARTITION struct {
Name string
Partition_index int
Partition string
Total_space_in_bytes string
Used_space_in_bytes string
Free_space_in_bytes string
Percentage_space_usage string
}
type PARTITIONLIST []PARTITION
type PARTITIONS struct {
Name string
List []PARTITIONS
}
/************ helper functions goes below here ************/
func bytesToSize(bytes uint64) string {
sizes := []string{"Bytes", "KB", "MB", "GB", "TB"}
if bytes == 0 {
return fmt.Sprint(float64(0), "bytes")
} else {
var bytes1 = float64(bytes)
var i = math.Floor(math.Log(bytes1) / math.Log(1024))
var count = bytes1 / math.Pow(1024, i)
var j = int(i)
var val = fmt.Sprintf("%.1f", count)
return fmt.Sprint(val, sizes[j])
}
}
func dealwithErr(err error) {
if err != nil {
fmt.Println(err)
}
}
EDIT: that error am getting on run time
unexpected newline, expecting comma or }
and that warning editor show on IDE
Cannot use partitionsList (type PARTITIONSLIST) as type []PARTITIONS
As the error clearly says:
Cannot use partitionsList (type PARTITIONSLIST) as type []PARTITIONS
You have type mismatch problem in the struct. Since PARTITIONSLIST is not qualy to []PARTITIONS. So if you create variable of both types they are different.
type PARTITIONLIST []PARTITION
type PARTITIONS struct {
Name string
List []PARTITIONS // here the list is slice of Partitions.
}
While when you are creating a slice of PARTITIONLIST type.
var partitionsList PARTITIONLIST // this is a variable of PARTITIONLIST type which is not equal to `[]PARTITIONS`
This is because golang is strictly typed language. So even if the underlying type of both values are similar. The are still different. To be more simple Try this example:
package main
import "fmt"
type MyInt int
func main() {
var a int = 2
var b MyInt = 2
fmt.Println(a==b)
}
Output:
invalid operation: a == b (mismatched types int and MyInt)
So you need to create a slice of []PARTITIONS
as:
var partitionsList `[]PARTITIONS`
or you can create both variables of PARTITIONLIST
type to make them similar.
Another error:
unexpected newline, expecting comma or }
is because you need to pass ,
after last field if you are using it in new line as:
var partitionDetails = PARTITIONS{
"partitions",
partitionsList, // pass comma here in your code.
}
Full working example :
package main
import (
"fmt"
"math"
"strconv"
"github.com/shirou/gopsutil/disk"
)
func main() {
/************ disk details goes here ************/
diskPartitions, err := disk.Partitions(true)
dealwithErr(err)
fmt.Println(diskPartitions)
var partitionsList PARTITIONLIST
for partitionIndex, partition := range diskPartitions {
partitionStat, err := disk.Usage(partition.Mountpoint)
dealwithErr(err)
var partitionDetails = PARTITION{
"PARTITION",
partitionIndex,
partition.Mountpoint,
"" + fmt.Sprint(partitionStat.Total) + " and " + bytesToSize(partitionStat.Total),
"" + fmt.Sprint(partitionStat.Used) + " and " + bytesToSize(partitionStat.Used),
"" + fmt.Sprint(partitionStat.Free) + " and " + bytesToSize(partitionStat.Free),
"" + fmt.Sprint(partitionStat.UsedPercent) + "and " + strconv.FormatFloat(partitionStat.UsedPercent, 'f', 2, 64),
}
partitionsList = append(partitionsList, partitionDetails)
}
//till here working fine
fmt.Println(partitionsList)
//THE BELOW TWO LINES ERROR IS THE ACTUAL ERROR I AM ASKING
var partitionDetails = PARTITIONS{
"partitions",
partitionsList,
}
fmt.Println(partitionDetails)
dealwithErr(err)
}
/************ all struct goes here ************/
type PARTITION struct {
Name string
Partition_index int
Partition string
Total_space_in_bytes string
Used_space_in_bytes string
Free_space_in_bytes string
Percentage_space_usage string
}
type PARTITIONLIST []PARTITION
type PARTITIONS struct {
Name string
List PARTITIONLIST
}
/************ helper functions goes below here ************/
func bytesToSize(bytes uint64) string {
sizes := []string{"Bytes", "KB", "MB", "GB", "TB"}
if bytes == 0 {
return fmt.Sprint(float64(0), "bytes")
} else {
var bytes1 = float64(bytes)
var i = math.Floor(math.Log(bytes1) / math.Log(1024))
var count = bytes1 / math.Pow(1024, i)
var j = int(i)
var val = fmt.Sprintf("%.1f", count)
return fmt.Sprint(val, sizes[j])
}
}
func dealwithErr(err error) {
if err != nil {
fmt.Println(err)
}
}
Consider, reading this and change your naming style,
Take time to decide what would be proper names.
you have declared PARITIONLIST
twice
type PARTITIONLIST []PARTITION //17th line, remove this
PARTITIONS
is defined as,
type PARTITIONS struct {
Name string
List []PARTITION
}
you can use PARTITIONLIST
instead of []PARTITION
type for List
field.
A struct variable field values are ended with comma,
var partitionDetails = PARTITIONS{
"partitions",
partitionsList,
}