Literal value in struct declaration










0















Say we have a struct like so:



type Foo struct 
one string
two int



is it possible to declare literal values for this, something like:



type Foo struct 
one "foobar"
two int



or



type Foo struct 
one string
two 5678



basically for some objects we might have a hardcoded value for a field.










share|improve this question



















  • 3





    It is not possible to declare a field as a value.

    – Cerise Limón
    Nov 16 '18 at 5:16












  • A field with a constant value has literally no purpose. Just use a package-level const.

    – Adrian
    Nov 16 '18 at 15:23











  • @Adrian that's not true, many objects might need a constant field, like a marker. In a Java class field you might have a field like String marker = "some uuid" and every instance of that class has that.

    – Alexander Mills
    Nov 17 '18 at 0:09












  • If every instance has the same value, it's not an instance value, it's a global constant. In which case you can just use a global constant. Remember, these are not classes.

    – Adrian
    Nov 19 '18 at 13:59















0















Say we have a struct like so:



type Foo struct 
one string
two int



is it possible to declare literal values for this, something like:



type Foo struct 
one "foobar"
two int



or



type Foo struct 
one string
two 5678



basically for some objects we might have a hardcoded value for a field.










share|improve this question



















  • 3





    It is not possible to declare a field as a value.

    – Cerise Limón
    Nov 16 '18 at 5:16












  • A field with a constant value has literally no purpose. Just use a package-level const.

    – Adrian
    Nov 16 '18 at 15:23











  • @Adrian that's not true, many objects might need a constant field, like a marker. In a Java class field you might have a field like String marker = "some uuid" and every instance of that class has that.

    – Alexander Mills
    Nov 17 '18 at 0:09












  • If every instance has the same value, it's not an instance value, it's a global constant. In which case you can just use a global constant. Remember, these are not classes.

    – Adrian
    Nov 19 '18 at 13:59













0












0








0


2






Say we have a struct like so:



type Foo struct 
one string
two int



is it possible to declare literal values for this, something like:



type Foo struct 
one "foobar"
two int



or



type Foo struct 
one string
two 5678



basically for some objects we might have a hardcoded value for a field.










share|improve this question
















Say we have a struct like so:



type Foo struct 
one string
two int



is it possible to declare literal values for this, something like:



type Foo struct 
one "foobar"
two int



or



type Foo struct 
one string
two 5678



basically for some objects we might have a hardcoded value for a field.







go struct default-value






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 6:21









lospejos

1,51221426




1,51221426










asked Nov 16 '18 at 4:51









Alexander MillsAlexander Mills

20k35163346




20k35163346







  • 3





    It is not possible to declare a field as a value.

    – Cerise Limón
    Nov 16 '18 at 5:16












  • A field with a constant value has literally no purpose. Just use a package-level const.

    – Adrian
    Nov 16 '18 at 15:23











  • @Adrian that's not true, many objects might need a constant field, like a marker. In a Java class field you might have a field like String marker = "some uuid" and every instance of that class has that.

    – Alexander Mills
    Nov 17 '18 at 0:09












  • If every instance has the same value, it's not an instance value, it's a global constant. In which case you can just use a global constant. Remember, these are not classes.

    – Adrian
    Nov 19 '18 at 13:59












  • 3





    It is not possible to declare a field as a value.

    – Cerise Limón
    Nov 16 '18 at 5:16












  • A field with a constant value has literally no purpose. Just use a package-level const.

    – Adrian
    Nov 16 '18 at 15:23











  • @Adrian that's not true, many objects might need a constant field, like a marker. In a Java class field you might have a field like String marker = "some uuid" and every instance of that class has that.

    – Alexander Mills
    Nov 17 '18 at 0:09












  • If every instance has the same value, it's not an instance value, it's a global constant. In which case you can just use a global constant. Remember, these are not classes.

    – Adrian
    Nov 19 '18 at 13:59







3




3





It is not possible to declare a field as a value.

– Cerise Limón
Nov 16 '18 at 5:16






It is not possible to declare a field as a value.

– Cerise Limón
Nov 16 '18 at 5:16














A field with a constant value has literally no purpose. Just use a package-level const.

– Adrian
Nov 16 '18 at 15:23





A field with a constant value has literally no purpose. Just use a package-level const.

– Adrian
Nov 16 '18 at 15:23













@Adrian that's not true, many objects might need a constant field, like a marker. In a Java class field you might have a field like String marker = "some uuid" and every instance of that class has that.

– Alexander Mills
Nov 17 '18 at 0:09






@Adrian that's not true, many objects might need a constant field, like a marker. In a Java class field you might have a field like String marker = "some uuid" and every instance of that class has that.

– Alexander Mills
Nov 17 '18 at 0:09














If every instance has the same value, it's not an instance value, it's a global constant. In which case you can just use a global constant. Remember, these are not classes.

– Adrian
Nov 19 '18 at 13:59





If every instance has the same value, it's not an instance value, it's a global constant. In which case you can just use a global constant. Remember, these are not classes.

– Adrian
Nov 19 '18 at 13:59












2 Answers
2






active

oldest

votes


















3














No, Go does not support literal types (a la TypeScript). Moreover, it actually can't, because of zero values.



Every type has a zero value which always exists and is always assignable to it. When variables of a type are declared, they are implicitly assigned to their type's zero value.



  • For integer (int, uint, int32, uint32, int64, uin64) or float (float32, float64) or complex (complex64 or complex128) types, this is just 0 (0.0 respectively).


  • For string types, this is the empty string "".


  • For slices, maps, pointers, channels, and interfaces, the zero value is nil.


  • For arrays (not slices: arrays are value-types with statically-known length); their zero value is just the zero value of the element type repeated to fill every slot


  • The zero value of a struct type is a struct all of whose fields are zero values


In any case, because it is always possible for any type to have a zero value, it would be incompatible to create a type which only allows for any particular non-zero value.



The best a literal type "foo" could possibly represent is that the value is either "foo" or the zero value "" (and no, Go doesn't support this anyway).



The closest you'll be able to do is a constant declaration, or a receiver function that just-so-happens to return a fixed value instead of an actual field.






share|improve this answer























  • well for example with Java, in a class declaration you can have a member that is initialized, String foo = "bar";

    – Alexander Mills
    Nov 16 '18 at 22:10







  • 1





    Go doesn't have default initialization, so that doing things like allocating slices, arrays, and structures (or just composite types in general) can always be as fast and simple as merely zeroing out the memory.

    – Nathan
    Nov 17 '18 at 6:59


















2














Go is statically typed language, meaning every variable need to be declared with specific data type.



Setting default value for each fields inside a struct on declaration is not possible in Go. it's not supported.



But maybe you can do something like this.



type Foo struct 
one string
two int


func NewFoo() *Foo
foo := new(Foo)
foo.one = "default value for one"
foo.two = 2
return foo


func main()
objectWithDefaultValueForItsField := NewFoo()



What I did is basically just created a function with name is
New<struct name>(). This function returns a new object with default value for each of the fields defined.




Notable exception, for object that created from anonymous struct, the default value of the fields can be specified on declaration. i.e.



obj := struct 
one string
two int

"default value for one",
2,






share|improve this answer

























  • well for example with Java, in a class declaration you can have a member that is initialized, String foo = "bar";

    – Alexander Mills
    Nov 16 '18 at 22:11










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331638%2fliteral-value-in-struct-declaration%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














No, Go does not support literal types (a la TypeScript). Moreover, it actually can't, because of zero values.



Every type has a zero value which always exists and is always assignable to it. When variables of a type are declared, they are implicitly assigned to their type's zero value.



  • For integer (int, uint, int32, uint32, int64, uin64) or float (float32, float64) or complex (complex64 or complex128) types, this is just 0 (0.0 respectively).


  • For string types, this is the empty string "".


  • For slices, maps, pointers, channels, and interfaces, the zero value is nil.


  • For arrays (not slices: arrays are value-types with statically-known length); their zero value is just the zero value of the element type repeated to fill every slot


  • The zero value of a struct type is a struct all of whose fields are zero values


In any case, because it is always possible for any type to have a zero value, it would be incompatible to create a type which only allows for any particular non-zero value.



The best a literal type "foo" could possibly represent is that the value is either "foo" or the zero value "" (and no, Go doesn't support this anyway).



The closest you'll be able to do is a constant declaration, or a receiver function that just-so-happens to return a fixed value instead of an actual field.






share|improve this answer























  • well for example with Java, in a class declaration you can have a member that is initialized, String foo = "bar";

    – Alexander Mills
    Nov 16 '18 at 22:10







  • 1





    Go doesn't have default initialization, so that doing things like allocating slices, arrays, and structures (or just composite types in general) can always be as fast and simple as merely zeroing out the memory.

    – Nathan
    Nov 17 '18 at 6:59















3














No, Go does not support literal types (a la TypeScript). Moreover, it actually can't, because of zero values.



Every type has a zero value which always exists and is always assignable to it. When variables of a type are declared, they are implicitly assigned to their type's zero value.



  • For integer (int, uint, int32, uint32, int64, uin64) or float (float32, float64) or complex (complex64 or complex128) types, this is just 0 (0.0 respectively).


  • For string types, this is the empty string "".


  • For slices, maps, pointers, channels, and interfaces, the zero value is nil.


  • For arrays (not slices: arrays are value-types with statically-known length); their zero value is just the zero value of the element type repeated to fill every slot


  • The zero value of a struct type is a struct all of whose fields are zero values


In any case, because it is always possible for any type to have a zero value, it would be incompatible to create a type which only allows for any particular non-zero value.



The best a literal type "foo" could possibly represent is that the value is either "foo" or the zero value "" (and no, Go doesn't support this anyway).



The closest you'll be able to do is a constant declaration, or a receiver function that just-so-happens to return a fixed value instead of an actual field.






share|improve this answer























  • well for example with Java, in a class declaration you can have a member that is initialized, String foo = "bar";

    – Alexander Mills
    Nov 16 '18 at 22:10







  • 1





    Go doesn't have default initialization, so that doing things like allocating slices, arrays, and structures (or just composite types in general) can always be as fast and simple as merely zeroing out the memory.

    – Nathan
    Nov 17 '18 at 6:59













3












3








3







No, Go does not support literal types (a la TypeScript). Moreover, it actually can't, because of zero values.



Every type has a zero value which always exists and is always assignable to it. When variables of a type are declared, they are implicitly assigned to their type's zero value.



  • For integer (int, uint, int32, uint32, int64, uin64) or float (float32, float64) or complex (complex64 or complex128) types, this is just 0 (0.0 respectively).


  • For string types, this is the empty string "".


  • For slices, maps, pointers, channels, and interfaces, the zero value is nil.


  • For arrays (not slices: arrays are value-types with statically-known length); their zero value is just the zero value of the element type repeated to fill every slot


  • The zero value of a struct type is a struct all of whose fields are zero values


In any case, because it is always possible for any type to have a zero value, it would be incompatible to create a type which only allows for any particular non-zero value.



The best a literal type "foo" could possibly represent is that the value is either "foo" or the zero value "" (and no, Go doesn't support this anyway).



The closest you'll be able to do is a constant declaration, or a receiver function that just-so-happens to return a fixed value instead of an actual field.






share|improve this answer













No, Go does not support literal types (a la TypeScript). Moreover, it actually can't, because of zero values.



Every type has a zero value which always exists and is always assignable to it. When variables of a type are declared, they are implicitly assigned to their type's zero value.



  • For integer (int, uint, int32, uint32, int64, uin64) or float (float32, float64) or complex (complex64 or complex128) types, this is just 0 (0.0 respectively).


  • For string types, this is the empty string "".


  • For slices, maps, pointers, channels, and interfaces, the zero value is nil.


  • For arrays (not slices: arrays are value-types with statically-known length); their zero value is just the zero value of the element type repeated to fill every slot


  • The zero value of a struct type is a struct all of whose fields are zero values


In any case, because it is always possible for any type to have a zero value, it would be incompatible to create a type which only allows for any particular non-zero value.



The best a literal type "foo" could possibly represent is that the value is either "foo" or the zero value "" (and no, Go doesn't support this anyway).



The closest you'll be able to do is a constant declaration, or a receiver function that just-so-happens to return a fixed value instead of an actual field.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 6:22









NathanNathan

464




464












  • well for example with Java, in a class declaration you can have a member that is initialized, String foo = "bar";

    – Alexander Mills
    Nov 16 '18 at 22:10







  • 1





    Go doesn't have default initialization, so that doing things like allocating slices, arrays, and structures (or just composite types in general) can always be as fast and simple as merely zeroing out the memory.

    – Nathan
    Nov 17 '18 at 6:59

















  • well for example with Java, in a class declaration you can have a member that is initialized, String foo = "bar";

    – Alexander Mills
    Nov 16 '18 at 22:10







  • 1





    Go doesn't have default initialization, so that doing things like allocating slices, arrays, and structures (or just composite types in general) can always be as fast and simple as merely zeroing out the memory.

    – Nathan
    Nov 17 '18 at 6:59
















well for example with Java, in a class declaration you can have a member that is initialized, String foo = "bar";

– Alexander Mills
Nov 16 '18 at 22:10






well for example with Java, in a class declaration you can have a member that is initialized, String foo = "bar";

– Alexander Mills
Nov 16 '18 at 22:10





1




1





Go doesn't have default initialization, so that doing things like allocating slices, arrays, and structures (or just composite types in general) can always be as fast and simple as merely zeroing out the memory.

– Nathan
Nov 17 '18 at 6:59





Go doesn't have default initialization, so that doing things like allocating slices, arrays, and structures (or just composite types in general) can always be as fast and simple as merely zeroing out the memory.

– Nathan
Nov 17 '18 at 6:59













2














Go is statically typed language, meaning every variable need to be declared with specific data type.



Setting default value for each fields inside a struct on declaration is not possible in Go. it's not supported.



But maybe you can do something like this.



type Foo struct 
one string
two int


func NewFoo() *Foo
foo := new(Foo)
foo.one = "default value for one"
foo.two = 2
return foo


func main()
objectWithDefaultValueForItsField := NewFoo()



What I did is basically just created a function with name is
New<struct name>(). This function returns a new object with default value for each of the fields defined.




Notable exception, for object that created from anonymous struct, the default value of the fields can be specified on declaration. i.e.



obj := struct 
one string
two int

"default value for one",
2,






share|improve this answer

























  • well for example with Java, in a class declaration you can have a member that is initialized, String foo = "bar";

    – Alexander Mills
    Nov 16 '18 at 22:11















2














Go is statically typed language, meaning every variable need to be declared with specific data type.



Setting default value for each fields inside a struct on declaration is not possible in Go. it's not supported.



But maybe you can do something like this.



type Foo struct 
one string
two int


func NewFoo() *Foo
foo := new(Foo)
foo.one = "default value for one"
foo.two = 2
return foo


func main()
objectWithDefaultValueForItsField := NewFoo()



What I did is basically just created a function with name is
New<struct name>(). This function returns a new object with default value for each of the fields defined.




Notable exception, for object that created from anonymous struct, the default value of the fields can be specified on declaration. i.e.



obj := struct 
one string
two int

"default value for one",
2,






share|improve this answer

























  • well for example with Java, in a class declaration you can have a member that is initialized, String foo = "bar";

    – Alexander Mills
    Nov 16 '18 at 22:11













2












2








2







Go is statically typed language, meaning every variable need to be declared with specific data type.



Setting default value for each fields inside a struct on declaration is not possible in Go. it's not supported.



But maybe you can do something like this.



type Foo struct 
one string
two int


func NewFoo() *Foo
foo := new(Foo)
foo.one = "default value for one"
foo.two = 2
return foo


func main()
objectWithDefaultValueForItsField := NewFoo()



What I did is basically just created a function with name is
New<struct name>(). This function returns a new object with default value for each of the fields defined.




Notable exception, for object that created from anonymous struct, the default value of the fields can be specified on declaration. i.e.



obj := struct 
one string
two int

"default value for one",
2,






share|improve this answer















Go is statically typed language, meaning every variable need to be declared with specific data type.



Setting default value for each fields inside a struct on declaration is not possible in Go. it's not supported.



But maybe you can do something like this.



type Foo struct 
one string
two int


func NewFoo() *Foo
foo := new(Foo)
foo.one = "default value for one"
foo.two = 2
return foo


func main()
objectWithDefaultValueForItsField := NewFoo()



What I did is basically just created a function with name is
New<struct name>(). This function returns a new object with default value for each of the fields defined.




Notable exception, for object that created from anonymous struct, the default value of the fields can be specified on declaration. i.e.



obj := struct 
one string
two int

"default value for one",
2,







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 6:25

























answered Nov 16 '18 at 6:10









xparexpare

4,7292252




4,7292252












  • well for example with Java, in a class declaration you can have a member that is initialized, String foo = "bar";

    – Alexander Mills
    Nov 16 '18 at 22:11

















  • well for example with Java, in a class declaration you can have a member that is initialized, String foo = "bar";

    – Alexander Mills
    Nov 16 '18 at 22:11
















well for example with Java, in a class declaration you can have a member that is initialized, String foo = "bar";

– Alexander Mills
Nov 16 '18 at 22:11





well for example with Java, in a class declaration you can have a member that is initialized, String foo = "bar";

– Alexander Mills
Nov 16 '18 at 22:11

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331638%2fliteral-value-in-struct-declaration%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党