关于Go语言结构体的语法问题

huangapple go评论82阅读模式
英文:

golang grammar questions about struct

问题

这里有一些代码,但是它很长而且不必要。有时我需要将一些东西写入到MySQL中,有一些类似的表格。

我一直在尝试使用interface{},但它更加复杂。

有没有办法让它更短一些?

type Device struct {
    Id      int
    Name    string
    Status  bool
    Devtype string
    // ...
    Created time.Time
}

func Insert(devtype string) {
    var value Device
    value.Id = 1
    value.Name = "device"
    value.Status = false
    value.Devtype = devtype
    value.Created = time.Now()
    fmt.Println(value)
}

你可以将OneTwoThree类型合并为一个名为Device的结构体类型。然后,在Insert函数中,只需创建一个Device变量,并根据devtype的值设置Devtype字段的不同值。这样可以减少重复的代码,并使代码更简洁。

英文:

here is some codes, but it is so long and unnecessary.
Sometimes I need to write somethings to mysql,
there is some kind of tables like that.

I have been try to use interface{}, but it is more complex.

Is there any way to make it shorter?

type One struct{
    Id int
    Name String
    Status bool
    Devtype string
    ...
    Created time.Time
}

type Two struct{
    Id int
    Name String
    Status bool
    Devtype string
    ...
    Created time.Time
}

type Three struct{
    Id int
    Name String
    Status bool
    Devtype string
    ...
    Created time.Time
}

func Insert(devtype string){
    if devtype == "one"{
        var value One
        value.Id = 1
        value.Name = "device"
        value.Status = false
        value.Devtype = devtype //only here is different
        value.Created = time.Now()
        fmt.Println(value)
    }else if devtype == "two"{
        var value Two
        value.Id = 1
        value.Name = "device"
        value.Status = false
        value.Devtype = devtype //only here is different
        value.Created = time.Now()
        fmt.Println(value)
    }else if devtype == "three"{
        var value Three
        value.Id = 1
        value.Name = "device"
        value.Status = false
        value.Devtype = devtype //only here is different
        value.Created = time.Now()
        fmt.Println(value)
    }
}

答案1

得分: 1

golang的结构体继承可以帮助实现这个需求。

type Base struct {
    Id int
    Name string
    Status bool
    ...
    Created time.Time
}

type One struct{
    Base
    Devtype string
}

type Two struct{
    Base
    Devtype string
}

type Three struct{
    Base
    Devtype string
}

func Insert(devtype string, base Base){
    switch devtype {
        case "one":
            var value One
            value.Base = base
            value.Devtype = devtype //只有这里不同
        case "two":
            var value Two
            value.Base = base
            value.Devtype = devtype //只有这里不同
        case "three":
            var value Three
            value.Base = base
            value.Devtype = devtype //只有这里不同
    }
}

注意:由于只有一个字段不同,创建三个类型并不是一个好的做法。

英文:

golang's struct inherit will help this

type Base struct {
    Id int
    Name String
    Status bool
    ...
    Created time.Time
}

type One struct{
	Base
    Devtype string
}

type Two struct{
	Base
    Devtype string
}

type Three struct{
	Base
    Devtype string
}

func Insert(devtype string, base Base){
	switch devtype {
		case "one"
			var value One
			value.Base = base
			value.Devtype = devtype //only here is different
		case "two"
			var value Two
			value.Base = base
			value.Devtype = devtype //only here is different
		case "three"
			var value Three
			value.Base = base
			value.Devtype = devtype //only here is different
	}
}

PS: since there is noly one field different, it is not a good practice to create three type

huangapple
  • 本文由 发表于 2016年12月16日 11:32:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/41176862.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定