我是 Go 的新手,我不明白为什么该变量tc.Duration没有被设置为全局变量的一部分Config.Core.Cron.Interval。我为测试添加了tc.x& tc.y,它们也没有设置。他们当然在方法中这样做,但我不明白为什么这没有进入全局变量。


为简洁起见,我已经稍微减少了代码,但我相信我仍然拥有所有相关的内容。


var Config Configuration


type Configuration struct {

    Core CoreConfig

}


type CoreConfig struct {

    Cron     CronConfig

}

type CronConfig struct{ Interval TimeConfig }


type TimeConfig struct {

    String   string

    Duration *time.Duration

    x        *int

    y        int

}


func (tc *TimeConfig) setDuration(errs *int) {

    if len(tc.String) > 0 {

        // var err error

        // d := time.Duration(0)

        z := 20

        tc.x = &z

        tc.y = z

        if d, err := time.ParseDuration(tc.String); err != nil {

            logger.Error(err)

            *errs++

        } else {

            tc.Duration = &d

        }

        spew.Dump(Config.Core.Cron.Interval)

    }

}


func (c Configuration) setSpecialValueTypes() error {

    var err error


    errs := 0

    p := &errs


    // Cron

    c.Core.Cron.Interval.String = `15m`

    c.Core.Cron.Interval.setDuration(p)


    if errs > 0 {

        err = errors.New(`Errors occurred while setting special values`)

    }


    return err

}