英文:
Overwriting an existing golang structure and adding a field
问题
func (c *client) Init(conf config.Config, topic string) (err error) {
defer func() {
p := recover()
switch v := p.(type) {
case error:
err = v
}
}()
c.reader = kafka.NewReader(kafka.ReaderConfig{
Brokers: conf.Brokers,
GroupID: conf.GroupID,
Topic: topic,
MaxWait: 1 * time.Second, // 最长等待新消息的时间
MinBytes: 1, // 最小消息大小
MaxBytes: 10e6, // 最大消息大小 1 MByte(Kafka 可处理的最大大小)
RetentionTime: time.Hour * 24 * 7, // ConsumerGroup 保留 1 周
WatchPartitionChanges: true, // 监听分区变化(例如增加分区)
})
if conf.TlsEnabled {
d := &kafka.Dialer{
TLS: &tls.Config{},
}
c.reader.Dialer = d
}
return err
}
英文:
func (c *client) Init(conf config.Config, topic string) (err error) {
defer func() {
p := recover()
switch v := p.(type) {
case error:
err = v
}
}()
c.reader = kafka.NewReader(kafka.ReaderConfig{
Brokers: conf.Brokers,
GroupID: conf.GroupID,
Topic: topic,
MaxWait: 1 * time.Second, // maximum time to wait for new messages
MinBytes: 1, // minimum message size
MaxBytes: 10e6, // maximum message size 1 MByte (= maximum size Kafka can handle)
RetentionTime: time.Hour * 24 * 7, // keep ConsumerGroup for 1 week
WatchPartitionChanges: true, // watch for changes to the partitions (e.g. increase of partitions)
})
if conf.TlsEnabled {
d := &kafka.Dialer{
TLS: &tls.Config{},
}
}
return err
}
Long story short: what I wanna do is adding the field Dialer: d
to c.reader
if TlsEnabled is true! c.reader is of type ReaderConfig which already contains Dialer field which is in my case:
d := &kafka.Dialer{
TLS: &tls.Config{},
}
答案1
得分: 2
如果我正确理解你的问题,你想在kafka.ReaderConfig
上设置Dialer
字段,但只有在conf.TlsEnabled
为true时才设置。在这种情况下,你应该在调用kafka.NewReader
之前移动if conf.TlsEnabled
的检查,并将kafka.ReaderConfig
赋值给一个变量,如下所示:
rConf := kafka.ReaderConfig{
Brokers: conf.Brokers,
GroupID: conf.GroupID,
Topic: topic,
MaxWait: 1 * time.Second, // maximum time to wait for new messages
MinBytes: 1, // minimum message size
MaxBytes: 10e6, // maximum message size 1 MByte (= maximum size Kafka can handle)
RetentionTime: time.Hour * 24 * 7, // keep ConsumerGroup for 1 week
WatchPartitionChanges: true, // watch for changes to the partitions (e.g. increase of partitions)
}
if conf.TlsEnabled {
rConf.Dialer = &kafka.Dialer{
TLS: &tls.Config{},
}
}
// now assign c.reader
c.reader = kafka.NewReader(rConf)
只是一个小问题:在Go语言中,首字母缩略词应该全部大写。你的配置类型不应该有一个名为TlsEnabled
的字段,而应该是TLSEnabled
。
英文:
If I understand your question correctly, you want to set the Dialer
field on kafka.ReaderConfig
if, and only if conf.TlsEnabled
is true. In that case, you should just move your if conf.TlsEnabled
check before you call kafka.NewReader
, and assign the kafka.ReaderConfig
to a variable, like so:
rConf := kafka.ReaderConfig{
Brokers: conf.Brokers,
GroupID: conf.GroupID,
Topic: topic,
MaxWait: 1 * time.Second, // maximum time to wait for new messages
MinBytes: 1, // minimum message size
MaxBytes: 10e6, // maximum message size 1 MByte (= maximum size Kafka can handle)
RetentionTime: time.Hour * 24 * 7, // keep ConsumerGroup for 1 week
WatchPartitionChanges: true, // watch for changes to the partitions (e.g. increase of partitions)
}
if conf.TlsEnabled {
rConf.Dialer = &kafka.Dialer{
TLS: &tls.Config{},
}
}
// now assign c.reader
c.reader = kafka.NewReader(rConf)
Just a small nit-pick: in golang, acronyms and other initialisms should be in all-caps. Your config type should not have a field called TlsEnabled
, but rather it should be TLSEnabled
.
答案2
得分: 0
你可以将client
嵌入到自定义类型中,但无法向现有类型添加字段。然后可以通过自定义类型直接访问这些字段。
type myType struct {
something string
foo int
bar int
}
type myExtendedType struct {
myType
dialer *kafka.Dialer
}
func main() {
ext := myExtendedType{
myType: myType{
something: "some",
foo: 24,
bar: 42,
},
dialer: &kafka.Dialer{
TLS: &tls.Config{},
},
}
println(ext.something, ext.foo, ext.bar, ext.dialer)
}
英文:
You can't add a field to an existing type but you can embed the client
in a custom type. The fields can then be accessed directly via the custom type
type myType struct {
something string
foo int
bar int
}
type myExtendedType struct {
myType
dialer *kafka.Dialer
}
func main() {
ext := myExtendedType{
myType: myType{
something: "some",
foo: 24,
bar: 42,
},
dialer: &kafka.Dialer{
TLS: &tls.Config{},
},
}
println(ext.something, ext.foo, ext.bar, ext.dialer)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论