'A structure that cannot be imported into a structure body
initialize/config.go
package initialize
type DatabaseConfig struct {
MysqlConfig MysqlConfig `yaml:"mysql"`
RedisConfig RedisConfig `yaml:"redis"`
}
type MysqlConfig struct {
Host string `yaml:"host"`
}
type RedisConfig struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
Password string `yaml:"password"`
Database string `yaml:"database"`
}
initialize/cache.go
package initialize
import (
"main/global"
"github.com/chenyahui/gin-cache/persist"
"github.com/go-redis/redis/v8"
)
func InitCache(r *DatabaseConfig.RedisConfig) {
// redisStore := persist.NewRedisStore(redis.NewClient(&redis.Options{
// Network: "tcp",
// Addr: "127.0.0.1:6379",
// }))
// global.Cache = redisStore
}
The InitCache
method in the cache.go
file accepts parameter Redis configuration, but this position is reported error. The error message is as follows: DatabaseConfig.RedisConfig undefined (type DatabaseConfig has no method RedisConfig)compilerMissingFieldOrMethod
Solution 1:[1]
Just pass DatabaseConfig
instead
func InitCache(c *DatabaseConfig) {
c.RedisConfig
}
Or
Directly pass the field
func InitCache(c *RedisConfig) {
//
}
// and in somewhere
var cfg DatabaseConfig
InitCache(&cfg.RedisConfig)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | hienduyph |