#Default Values for Structs not working

43 messages · Page 1 of 1 (latest)

vital pier
#

Im having a problems with the default values for this struct

type textWrapper struct {
wrapper_symbol string default:"#"
filler_symbol string default:" "
lines_length int default:"80"
lines_per_screen int default:"10"
}

When checking in main if the default is set the value is 0

func main() {
message_wrapper := textWrapper{}
fmt.Println("Set Lines per Screen:", message_wrapper.lines_per_screen)

var welcome_message string = "Hello World\n What is up ???"
message_wrapper.wrapMessage(welcome_message)

}

Am I doing something wrong with the default values ?
Looked it up on many websites and it seems like the right way to do it ...
Thanks for the help

valid vessel
#

What library are you using? Go doesnt have default values (not to be confused with zero values) or default: tag bu default

vital pier
#

I am not using any library ... From what i looked up it seemed like you can specify default values with the tags inside the ` gravis accent
So there is no way to set default values in standard go ?
If so is there a library you could recommend ?

tribal bridge
#

@vital pier are you using halucinogenic pieces of maths to teach you go ?

#

Because that just not a thing

#

the default value for everyhing is 0

vital pier
#

I wish haha . Yeah i figured by text debugging, but several websites, stackoverflow etc. you name em, had solutions with actual default values added for structs in gravis accent. Now I am using a constructor but that does not really statisfy me xD Really wanna avoid that

tribal bridge
#

the backits literally do nothing from a language point of view

#

this is some strings that is accessible through reflect

#

assuming this isn't chatgpt's invention, my guess is that a special serialization lib supports it

vital pier
#

Maybe it is a library although no website has mentioned a library to set default values, it seemed to be built-in. Also if I think about any type go has also default values for them so it should be possible by the standard library

tribal bridge
#

the way the std implements default values is by "making the zero value useful"

#

let's say you want to make a custom int that defaults to 42 then you can write:

type Int42 struct{
 v int
 set bool
}

func (i Int42) Get() int {
 if !i.set { return 42 }
 return i.v
}

func (i *Int42) Set(v int) {
 i.v = v
 i.set = true
}
#

usually this is done using sync.Once and or by checking == nil

vital pier
#

so idiomatically you would use getters and setter for that case ?

tribal bridge
#

idk

#

I wouldn't rely on magic default values

#

or if you really want then I might use a constructor

#

like do:

func newTextWrapper() textWrapper {
 return textWrapper{"#", " ", 80, 10}
}
#

And then always use newTextWrapper

fiery blaze
#

Tags does not provide static check, so do not use tags for default value

#

instead define a defaultTypeName variable, and assign the value from it

fiery blaze
tribal bridge
#

¯_(ツ)_/¯

#

I avoid returning pointers because of the memory escaping issues

vital pier
vital pier
#

how to format code in discord btw ? xD

tribal bridge
fiery blaze
#

You should do this

type textWrapper struct {
    wrapper_symbol   string
    filler_symbol    string
    lines_length     int
    lines_per_screen int
}

// note that your struct should not contains any allocated pointer, or you will need a Clone() method
var defaultTextWrapper = textWrapper{
  wrapper_symbol:   "#",
  filler_symbol:    " ",
  lines_length:     80,
  lines_per_screen: 10,
}

func main() {
    message_wrapper := defaultTextWrapper
    fmt.Println("Set Lines per Screen:", message_wrapper.lines_per_screen)

    var welcome_message string = "Hello World\n What is up ???"
    message_wrapper.wrapMessage(welcome_message)
}
tribal bridge
#

@fiery blaze why say that ?

#

this code hardly makes sense

#

your default tag isn't valid syntax, and isn't even used (you would need to call reflect)

fiery blaze
#

I just copy and paste his code and forgot to remove them lol

tribal bridge
#

❤️

#

I wouldn't rely on a global variable, putting it inside a function make sure no one try to sneakly edit the default values.
That has a big part of personal taste.

vital pier
#

Gotta admit im confused by alot of golangs syntax and language decisions

tribal bridge
#

like I wouldn't try a third party lib that add a language feature that isn't existant

fiery blaze
vital pier
#

I mean i could basically check for default values of types and then assign default values, but that could be potentionally alot of checks just for a missing language feature. How did nobody at google think about that I wonder