【问题标题】:Structs strange behavior构造奇怪的行为
【发布时间】:2019-03-09 12:31:41
【问题描述】:

我刚开始使用 Go-lang 并遇到了它的 Structs 的奇怪行为。我有一个结构A 和另一个结构B,在结构B 一个键定义为[]A 问题是当分配B 的新实例类型的值作为A 的元素时它抛出错误尽管类型相同。任何帮助将不胜感激 下面我粘贴了导致错误的最小代码

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

编辑:该错误正在运行时出现

意外的换行符,需要逗号或 }

那个警告编辑器会显示在 IDE 上

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

【问题讨论】:

  • 请注意,在我开始使用slices 生成动态分区列表之前,所有结构都有效
  • 你的问题是不必要的混乱。您的代码中没有 A 和 B 结构,因此只需使用您在代码中的名称,以便清楚您在说什么。此外,如果您有错误,请复制粘贴该错误以便我们查看。
  • @AbdulHameed 请发布您遇到的错误。因为我认为这是由于类型不匹配造成的。
  • 其实是非常大的单个文件。我刚刚发布了生成错误的代码示例
  • @AbdulHameed 事情是我能看到的是你的类型不匹配。

标签: go struct


【解决方案1】:

考虑一下,阅读 this 并更改您的命名风格,

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

你已经声明了两次PARITIONLIST

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

PARTITIONS 定义为,

type PARTITIONS struct {
    Name string
    List []PARTITION
}

对于List 字段,您可以使用PARTITIONLIST 而不是[]PARTITION 类型。

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

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

【讨论】:

  • @AbdulHameed,如前所述,输入PARTITIONS 字段ListPARTITIONLIST 类型而不是[]PARTITION。考虑仔细阅读答案。
【解决方案2】:

正如错误清楚地表明:

不能使用 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

【讨论】:

  • 当底层类型相同时,可以赋值,example
  • @nilsocket 不,你不能因为 go 是严格的。您正在做的是您没有创建不同类型的变量。您正在创建一个原始类型的变量。
  • 这是更新后的example,不能将其传递给函数,但可以在结构中赋值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2015-05-07
  • 2017-04-24
相关资源
最近更新 更多