Go:我获得了一个结构体字段值,但没有引用该结构体类型的指针变量,为什么?
// config/config.go
package config
import (
"github.com/spf13/viper"
)
type Config struct {
Database DatabaseConfig `mapstructure:"database"`
Server ServerConfig `mapstructure:"server"`
Redis RedisConfig `mapstructure:"redis"`
Jwt JwtConfig `mapstructure:"jwt"`
Smtp SmtpConfig `mapstructure:"smtp"`
}
func New() (config *Config) {
viper.AddConfigPath(".")
viper.SetConfigName("config")
if err := viper.ReadInConfig(); err != nil {
panic(err)
}
if err := viper.Unmarshal(&config); err != nil {
panic(err)
}
return
}
// db/connection.go
package db
import (
"fmt"
"my.project/config"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func New(config config.DatabaseConfig) *gorm.DB {
dsn := fmt.Sprintf(
"%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
config.Username, config.Password, config.Host, config.Port, config.Database,
)
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
fmt.Println("Failed to connect to database")
panic(err)
}
RunMigrations(db)
return db
}
// main.go
package main
import (
"fmt"
"my.project/config"
)
func main() {
config := config.New()
fmt.Println(config.Database)
db := db.New(config.Database)
if db == nil {
panic("Failed to connect to database")
}
}
在上面的代码片段中,config
是一个指向Config类型变量的指针(显然来自config.New()
的返回类型),但是config.Database< /code> 似乎是一个值(而不是引用)。
> 是一个值)。
我假设 config.Database 也将是一个引用,但事实并非如此(我尝试打印它们,config
绝对是一个引用,而 config.Database
谁能帮助我理解为什么?先感谢您!
// config/config.go
package config
import (
"github.com/spf13/viper"
)
type Config struct {
Database DatabaseConfig `mapstructure:"database"`
Server ServerConfig `mapstructure:"server"`
Redis RedisConfig `mapstructure:"redis"`
Jwt JwtConfig `mapstructure:"jwt"`
Smtp SmtpConfig `mapstructure:"smtp"`
}
func New() (config *Config) {
viper.AddConfigPath(".")
viper.SetConfigName("config")
if err := viper.ReadInConfig(); err != nil {
panic(err)
}
if err := viper.Unmarshal(&config); err != nil {
panic(err)
}
return
}
// db/connection.go
package db
import (
"fmt"
"my.project/config"
"gorm.io/driver/mysql"
"gorm.io/gorm"
)
func New(config config.DatabaseConfig) *gorm.DB {
dsn := fmt.Sprintf(
"%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
config.Username, config.Password, config.Host, config.Port, config.Database,
)
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
fmt.Println("Failed to connect to database")
panic(err)
}
RunMigrations(db)
return db
}
// main.go
package main
import (
"fmt"
"my.project/config"
)
func main() {
config := config.New()
fmt.Println(config.Database)
db := db.New(config.Database)
if db == nil {
panic("Failed to connect to database")
}
}
In the above snippets, config
is a pointer to a Config type variable (apparently from the return type of config.New()
), but config.Database
seems to be a value (not a reference).
I assumed config.Database
would be a reference either but it's not (I tried to print out them, config
was definitely a reference and config.Database
was a value).
Can anyone help me to understand why? Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,这是源于语言设计决策之一。事实上,如果你使用
(*config).Database
,它是绝对有效的。然而,Go 的开发者认为这种表示法很麻烦,因此该语言允许我们编写config.Database
,没有明确的取消引用。这些指向结构的指针甚至有自己的名称“结构指针”,并且它们会自动取消引用。这是“自动引用”的另一个精彩解释:Go 的指针何时取消引用自身
It turned out that this was originated from one of the language design decisions. In fact, if you use
(*config).Database
, it is absolutely valid. However, the makers of Go deemed this notation cumbersome, so the language permits us to writeconfig.Database
,without an explicit dereference. These pointers to structs even have their own name
struct pointers
and they are automatically dereferenced. Here's another superb explanation for "auto-deferencing": When do Go's pointers dereference themselves