Literal value in struct declaration
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
add a comment |
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
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-levelconst
.
– 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 likeString 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
add a comment |
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
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
go struct default-value
edited Nov 16 '18 at 6:21
lospejos
1,51021426
1,51021426
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-levelconst
.
– 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 likeString 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
add a comment |
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-levelconst
.
– 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 likeString 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
add a comment |
2 Answers
2
active
oldest
votes
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
orcomplex128
) types, this is just0
(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 const
ant declaration, or a receiver function that just-so-happens to return a fixed value instead of an actual field.
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
add a comment |
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 isNew<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,
}
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
orcomplex128
) types, this is just0
(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 const
ant declaration, or a receiver function that just-so-happens to return a fixed value instead of an actual field.
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
add a comment |
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
orcomplex128
) types, this is just0
(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 const
ant declaration, or a receiver function that just-so-happens to return a fixed value instead of an actual field.
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
add a comment |
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
orcomplex128
) types, this is just0
(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 const
ant declaration, or a receiver function that just-so-happens to return a fixed value instead of an actual field.
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
orcomplex128
) types, this is just0
(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 const
ant declaration, or a receiver function that just-so-happens to return a fixed value instead of an actual field.
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
add a comment |
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
add a comment |
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 isNew<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,
}
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
add a comment |
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 isNew<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,
}
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
add a comment |
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 isNew<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,
}
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 isNew<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,
}
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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