构造奇怪的行为

时间:2018-10-04 07:25:51

标签: go struct

我刚开始玩Go-lang,遇到了Structs的奇怪行为。我有一个Struct A和另一个Struct B,在Struct B中,一个键定义为[]A,问题是分配新实例类型{{1} }作为B的元素,尽管类型相同,但仍会引发错误。任何帮助将不胜感激 在下面,我粘贴了导致错误的最小代码

A

编辑:该错误正在运行时

  

意外的换行符,期望逗号或}

并且警告编辑器显示在IDE上

  

不能将partitionsList(类型PARTITIONSLIST)用作[] PARTITIONS

2 个答案:

答案 0 :(得分:1)

考虑,阅读this并更改命名方式,

花时间决定什么是专有名称。

您两次声明PARITIONLIST

type PARTITIONLIST []PARTITION //17th line, remove this

PARTITIONS被定义为

type PARTITIONS struct {
    Name string
    List []PARTITION
}

您可以在PARTITIONLIST字段中使用[]PARTITION而不是List类型。

结构变量字段值以逗号结尾,

var partitionDetails = PARTITIONS{
        "partitions",
        partitionsList,
    }

答案 1 :(得分:1)

该错误清楚地表明:

  

不能将partitionsList(类型PARTITIONSLIST)用作[] PARTITIONS

您在结构中存在类型不匹配问题。由于PARTITIONSLIST不适合[] PARTITIONS。因此,如果您创建这两种类型的变量,则它们是不同的。

type PARTITIONLIST []PARTITION

type PARTITIONS struct {
    Name                string
    List                []PARTITIONS // here the list is slice of Partitions.
}

在创建PARTITIONLIST类型的切片时。

var partitionsList PARTITIONLIST // this is a variable of PARTITIONLIST type which is not equal to `[]PARTITIONS`

这是因为golang是严格键入的语言。因此,即使两个值的基础类型相似。仍然不同。为了更简单,请尝试以下示例:

package main

import "fmt"

type MyInt int

func main() {
    var a int = 2
    var b MyInt = 2
    fmt.Println(a==b)
}

输出:

  

无效操作:a == b(类型不匹配的int和MyInt)

Playground Example

因此,您需要创建[]PARTITIONS的一部分,作为:

var partitionsList `[]PARTITIONS`

或者您可以创建两个PARTITIONLIST类型的变量以使其相似。

另一个错误:

  

意外的换行符,期望逗号或}

是因为如果在新行中使用,,则需要在最后一个字段之后传递:

var partitionDetails = PARTITIONS{
    "partitions",
    partitionsList, // pass comma here in your code.
}

完整的示例:

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

Playground example