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