Converting a type to an array - GO syntaxConvert string to integer type in Go?How to assign string to bytes arrayHow to convert an int value to string in Go?Strange type definition syntax in Golang (name, then type, then string literal)Type converting slices of interfacesHow to convert a zero-terminated byte array to string?cannot convert data (type interface ) to type string: need type assertionHow to find a type of an object in Go?Convert array to slice in GoFunction declaration syntax: things in parenthesis before function name
Probability of rolling a specific combination of results with nd6
What was the deeper meaning of Hermione wanting the cloak?
Get the encrypted payload from an unencrypted wrapper PDF document
What is the origin of the "being immortal sucks" trope?
Is it safe to unplug a blinking USB drive after 'safely' ejecting it?
Should the pagination be reset when changing the order?
Applications of mathematics in clinical setting
Most efficient way to convert from 3.5-4.2V (singe cell LiPo) to stable 3.3V for currents up to 500 mA?
Inquiry answerer
What's the word for a student who doesn't register but goes to a class anyway?
Is a global DNS record a security risk for phpMyAdmin?
Why does Canada require a minimum rate of climb for ultralights of 300 ft/min?
How often is duct tape used during crewed space missions?
What's the purpose of autocorrelation?
How could artificial intelligence harm us?
Which museums have artworks of all four ninja turtles' namesakes?
Do household ovens ventilate heat to the outdoors?
Can we store a whole string with the help of a single character pointer? If yes , How?
Debussy as term for bathroom?
Is there a connection between IT and Ghostbusters?
Is it possible that the shadow of The Moon is a single dot during solar eclipse?
Are lay articles good enough to be the main source of information for PhD research?
Dear Fellow PSE Users,
Is there any actual security benefit to restricting foreign IPs?
Converting a type to an array - GO syntax
Convert string to integer type in Go?How to assign string to bytes arrayHow to convert an int value to string in Go?Strange type definition syntax in Golang (name, then type, then string literal)Type converting slices of interfacesHow to convert a zero-terminated byte array to string?cannot convert data (type interface ) to type string: need type assertionHow to find a type of an object in Go?Convert array to slice in GoFunction declaration syntax: things in parenthesis before function name
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
In the below syntax_1,
array := [...]float647.0, 8.5, 9.1
and syntax_2,
type People interface
SayHello()
ToString()
type Student struct
Person
university string
course string
type Developer struct
Person
company string
platform string
func main()
alex := StudentPerson"alex", 21, "111-222-XXX", "MIT","BS CS"
john := DeveloperPerson"John", 35, "111-222-XXX", "Accel North America", "Golang"
jithesh := DeveloperPerson"Jithesh", 33, "111-222-XXX", "Accel North America", "Hadoop"
//An array with People types
peopleArr := [...]Peoplealex, john,jithesh
1) What does this syntax float647.0, 8.5, 9.1 & Peoplealex, john,jithesh mean? this looks like a paradigm(a way of programming) more than a syntax
2) Can you provide reference to the meaning/purpose of [...] syntax? I see converting something to an [] type
go
|
show 6 more comments
In the below syntax_1,
array := [...]float647.0, 8.5, 9.1
and syntax_2,
type People interface
SayHello()
ToString()
type Student struct
Person
university string
course string
type Developer struct
Person
company string
platform string
func main()
alex := StudentPerson"alex", 21, "111-222-XXX", "MIT","BS CS"
john := DeveloperPerson"John", 35, "111-222-XXX", "Accel North America", "Golang"
jithesh := DeveloperPerson"Jithesh", 33, "111-222-XXX", "Accel North America", "Hadoop"
//An array with People types
peopleArr := [...]Peoplealex, john,jithesh
1) What does this syntax float647.0, 8.5, 9.1 & Peoplealex, john,jithesh mean? this looks like a paradigm(a way of programming) more than a syntax
2) Can you provide reference to the meaning/purpose of [...] syntax? I see converting something to an [] type
go
4
It's all here: golang.org/ref/spec. 1) A composite literal, it is syntax, not "paradigm". 2) Create an array whose length is the same as the number of elements provided to the literal.
– mkopriva
Mar 28 at 14:17
@mkopriva golang.org/search?q=%5B...%5D
– overexchange
Mar 28 at 14:17
2
1, 2, 3is a composite literal, and "The notation...specifies an array length equal to the maximum element index plus one."
– icza
Mar 28 at 14:18
1
@overexchange what are you implying with that link? That it's hard to find? Try the link in my first comment and try searching that page with CTRL/CMD+F, that's more efficient.
– mkopriva
Mar 28 at 14:20
1
Unnamed arrays* can't implement interfaces. It's objects that implement interfaces, so you have an array of objects and those objects implement the interface. Edit: I use objects here, but it may be more correct to say structs.
– Devon
Mar 28 at 14:31
|
show 6 more comments
In the below syntax_1,
array := [...]float647.0, 8.5, 9.1
and syntax_2,
type People interface
SayHello()
ToString()
type Student struct
Person
university string
course string
type Developer struct
Person
company string
platform string
func main()
alex := StudentPerson"alex", 21, "111-222-XXX", "MIT","BS CS"
john := DeveloperPerson"John", 35, "111-222-XXX", "Accel North America", "Golang"
jithesh := DeveloperPerson"Jithesh", 33, "111-222-XXX", "Accel North America", "Hadoop"
//An array with People types
peopleArr := [...]Peoplealex, john,jithesh
1) What does this syntax float647.0, 8.5, 9.1 & Peoplealex, john,jithesh mean? this looks like a paradigm(a way of programming) more than a syntax
2) Can you provide reference to the meaning/purpose of [...] syntax? I see converting something to an [] type
go
In the below syntax_1,
array := [...]float647.0, 8.5, 9.1
and syntax_2,
type People interface
SayHello()
ToString()
type Student struct
Person
university string
course string
type Developer struct
Person
company string
platform string
func main()
alex := StudentPerson"alex", 21, "111-222-XXX", "MIT","BS CS"
john := DeveloperPerson"John", 35, "111-222-XXX", "Accel North America", "Golang"
jithesh := DeveloperPerson"Jithesh", 33, "111-222-XXX", "Accel North America", "Hadoop"
//An array with People types
peopleArr := [...]Peoplealex, john,jithesh
1) What does this syntax float647.0, 8.5, 9.1 & Peoplealex, john,jithesh mean? this looks like a paradigm(a way of programming) more than a syntax
2) Can you provide reference to the meaning/purpose of [...] syntax? I see converting something to an [] type
go
go
edited Mar 28 at 14:17
overexchange
asked Mar 28 at 14:16
overexchangeoverexchange
4,2337 gold badges37 silver badges96 bronze badges
4,2337 gold badges37 silver badges96 bronze badges
4
It's all here: golang.org/ref/spec. 1) A composite literal, it is syntax, not "paradigm". 2) Create an array whose length is the same as the number of elements provided to the literal.
– mkopriva
Mar 28 at 14:17
@mkopriva golang.org/search?q=%5B...%5D
– overexchange
Mar 28 at 14:17
2
1, 2, 3is a composite literal, and "The notation...specifies an array length equal to the maximum element index plus one."
– icza
Mar 28 at 14:18
1
@overexchange what are you implying with that link? That it's hard to find? Try the link in my first comment and try searching that page with CTRL/CMD+F, that's more efficient.
– mkopriva
Mar 28 at 14:20
1
Unnamed arrays* can't implement interfaces. It's objects that implement interfaces, so you have an array of objects and those objects implement the interface. Edit: I use objects here, but it may be more correct to say structs.
– Devon
Mar 28 at 14:31
|
show 6 more comments
4
It's all here: golang.org/ref/spec. 1) A composite literal, it is syntax, not "paradigm". 2) Create an array whose length is the same as the number of elements provided to the literal.
– mkopriva
Mar 28 at 14:17
@mkopriva golang.org/search?q=%5B...%5D
– overexchange
Mar 28 at 14:17
2
1, 2, 3is a composite literal, and "The notation...specifies an array length equal to the maximum element index plus one."
– icza
Mar 28 at 14:18
1
@overexchange what are you implying with that link? That it's hard to find? Try the link in my first comment and try searching that page with CTRL/CMD+F, that's more efficient.
– mkopriva
Mar 28 at 14:20
1
Unnamed arrays* can't implement interfaces. It's objects that implement interfaces, so you have an array of objects and those objects implement the interface. Edit: I use objects here, but it may be more correct to say structs.
– Devon
Mar 28 at 14:31
4
4
It's all here: golang.org/ref/spec. 1) A composite literal, it is syntax, not "paradigm". 2) Create an array whose length is the same as the number of elements provided to the literal.
– mkopriva
Mar 28 at 14:17
It's all here: golang.org/ref/spec. 1) A composite literal, it is syntax, not "paradigm". 2) Create an array whose length is the same as the number of elements provided to the literal.
– mkopriva
Mar 28 at 14:17
@mkopriva golang.org/search?q=%5B...%5D
– overexchange
Mar 28 at 14:17
@mkopriva golang.org/search?q=%5B...%5D
– overexchange
Mar 28 at 14:17
2
2
1, 2, 3 is a composite literal, and "The notation ... specifies an array length equal to the maximum element index plus one."– icza
Mar 28 at 14:18
1, 2, 3 is a composite literal, and "The notation ... specifies an array length equal to the maximum element index plus one."– icza
Mar 28 at 14:18
1
1
@overexchange what are you implying with that link? That it's hard to find? Try the link in my first comment and try searching that page with CTRL/CMD+F, that's more efficient.
– mkopriva
Mar 28 at 14:20
@overexchange what are you implying with that link? That it's hard to find? Try the link in my first comment and try searching that page with CTRL/CMD+F, that's more efficient.
– mkopriva
Mar 28 at 14:20
1
1
Unnamed arrays* can't implement interfaces. It's objects that implement interfaces, so you have an array of objects and those objects implement the interface. Edit: I use objects here, but it may be more correct to say structs.
– Devon
Mar 28 at 14:31
Unnamed arrays* can't implement interfaces. It's objects that implement interfaces, so you have an array of objects and those objects implement the interface. Edit: I use objects here, but it may be more correct to say structs.
– Devon
Mar 28 at 14:31
|
show 6 more comments
1 Answer
1
active
oldest
votes
Squiggly brackets are used to define a composite literal. A value that contains more than one value inside it. In your first example an array of float values is created[...]float647.0, 8.5, 9.1 is a literal for an array of floats with 3 elements. In your second example, a few literals for predefined struct types are created and another array. Person"alex", 21, "111-222-XXX" is a literal for a struct of type Person. And [...]Peoplealex, john,jithesh is an array of type People containing 3 elements.
Can I say ,Peoplealex, john, jiteshis a composite literal for a struct of typePeople? where namealexis pointing to a literal(object) for a struct of typeStudent?
– overexchange
Mar 28 at 15:15
People type is an interface, so anything that implements this interface can be a member of this array. For this code to work both Developer and Student need to implement People interface either directly or through the embedded Person struct.
– Mad Wombat
Mar 28 at 15:20
1
ButPeoplealex, john, jiteshon its own is not valid. It needs[...]to make it an array of People. Or simply[]to make it a slice of People.
– Mad Wombat
Mar 28 at 15:21
[...]Peopleis an array.[]Peopleis a slice. These are two different types.
– Mad Wombat
Mar 28 at 15:23
[...]Peoplewould create an empty array of zero elements, but[...]Peoplealex, john, jiteshwould create a populated array of three elements
– Mad Wombat
Mar 28 at 15:24
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/4.0/"u003ecc by-sa 4.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%2f55399823%2fconverting-a-type-to-an-array-go-syntax%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Squiggly brackets are used to define a composite literal. A value that contains more than one value inside it. In your first example an array of float values is created[...]float647.0, 8.5, 9.1 is a literal for an array of floats with 3 elements. In your second example, a few literals for predefined struct types are created and another array. Person"alex", 21, "111-222-XXX" is a literal for a struct of type Person. And [...]Peoplealex, john,jithesh is an array of type People containing 3 elements.
Can I say ,Peoplealex, john, jiteshis a composite literal for a struct of typePeople? where namealexis pointing to a literal(object) for a struct of typeStudent?
– overexchange
Mar 28 at 15:15
People type is an interface, so anything that implements this interface can be a member of this array. For this code to work both Developer and Student need to implement People interface either directly or through the embedded Person struct.
– Mad Wombat
Mar 28 at 15:20
1
ButPeoplealex, john, jiteshon its own is not valid. It needs[...]to make it an array of People. Or simply[]to make it a slice of People.
– Mad Wombat
Mar 28 at 15:21
[...]Peopleis an array.[]Peopleis a slice. These are two different types.
– Mad Wombat
Mar 28 at 15:23
[...]Peoplewould create an empty array of zero elements, but[...]Peoplealex, john, jiteshwould create a populated array of three elements
– Mad Wombat
Mar 28 at 15:24
add a comment
|
Squiggly brackets are used to define a composite literal. A value that contains more than one value inside it. In your first example an array of float values is created[...]float647.0, 8.5, 9.1 is a literal for an array of floats with 3 elements. In your second example, a few literals for predefined struct types are created and another array. Person"alex", 21, "111-222-XXX" is a literal for a struct of type Person. And [...]Peoplealex, john,jithesh is an array of type People containing 3 elements.
Can I say ,Peoplealex, john, jiteshis a composite literal for a struct of typePeople? where namealexis pointing to a literal(object) for a struct of typeStudent?
– overexchange
Mar 28 at 15:15
People type is an interface, so anything that implements this interface can be a member of this array. For this code to work both Developer and Student need to implement People interface either directly or through the embedded Person struct.
– Mad Wombat
Mar 28 at 15:20
1
ButPeoplealex, john, jiteshon its own is not valid. It needs[...]to make it an array of People. Or simply[]to make it a slice of People.
– Mad Wombat
Mar 28 at 15:21
[...]Peopleis an array.[]Peopleis a slice. These are two different types.
– Mad Wombat
Mar 28 at 15:23
[...]Peoplewould create an empty array of zero elements, but[...]Peoplealex, john, jiteshwould create a populated array of three elements
– Mad Wombat
Mar 28 at 15:24
add a comment
|
Squiggly brackets are used to define a composite literal. A value that contains more than one value inside it. In your first example an array of float values is created[...]float647.0, 8.5, 9.1 is a literal for an array of floats with 3 elements. In your second example, a few literals for predefined struct types are created and another array. Person"alex", 21, "111-222-XXX" is a literal for a struct of type Person. And [...]Peoplealex, john,jithesh is an array of type People containing 3 elements.
Squiggly brackets are used to define a composite literal. A value that contains more than one value inside it. In your first example an array of float values is created[...]float647.0, 8.5, 9.1 is a literal for an array of floats with 3 elements. In your second example, a few literals for predefined struct types are created and another array. Person"alex", 21, "111-222-XXX" is a literal for a struct of type Person. And [...]Peoplealex, john,jithesh is an array of type People containing 3 elements.
answered Mar 28 at 14:44
Mad WombatMad Wombat
7,3315 gold badges42 silver badges75 bronze badges
7,3315 gold badges42 silver badges75 bronze badges
Can I say ,Peoplealex, john, jiteshis a composite literal for a struct of typePeople? where namealexis pointing to a literal(object) for a struct of typeStudent?
– overexchange
Mar 28 at 15:15
People type is an interface, so anything that implements this interface can be a member of this array. For this code to work both Developer and Student need to implement People interface either directly or through the embedded Person struct.
– Mad Wombat
Mar 28 at 15:20
1
ButPeoplealex, john, jiteshon its own is not valid. It needs[...]to make it an array of People. Or simply[]to make it a slice of People.
– Mad Wombat
Mar 28 at 15:21
[...]Peopleis an array.[]Peopleis a slice. These are two different types.
– Mad Wombat
Mar 28 at 15:23
[...]Peoplewould create an empty array of zero elements, but[...]Peoplealex, john, jiteshwould create a populated array of three elements
– Mad Wombat
Mar 28 at 15:24
add a comment
|
Can I say ,Peoplealex, john, jiteshis a composite literal for a struct of typePeople? where namealexis pointing to a literal(object) for a struct of typeStudent?
– overexchange
Mar 28 at 15:15
People type is an interface, so anything that implements this interface can be a member of this array. For this code to work both Developer and Student need to implement People interface either directly or through the embedded Person struct.
– Mad Wombat
Mar 28 at 15:20
1
ButPeoplealex, john, jiteshon its own is not valid. It needs[...]to make it an array of People. Or simply[]to make it a slice of People.
– Mad Wombat
Mar 28 at 15:21
[...]Peopleis an array.[]Peopleis a slice. These are two different types.
– Mad Wombat
Mar 28 at 15:23
[...]Peoplewould create an empty array of zero elements, but[...]Peoplealex, john, jiteshwould create a populated array of three elements
– Mad Wombat
Mar 28 at 15:24
Can I say ,
Peoplealex, john, jitesh is a composite literal for a struct of type People? where name alex is pointing to a literal(object) for a struct of type Student?– overexchange
Mar 28 at 15:15
Can I say ,
Peoplealex, john, jitesh is a composite literal for a struct of type People? where name alex is pointing to a literal(object) for a struct of type Student?– overexchange
Mar 28 at 15:15
People type is an interface, so anything that implements this interface can be a member of this array. For this code to work both Developer and Student need to implement People interface either directly or through the embedded Person struct.
– Mad Wombat
Mar 28 at 15:20
People type is an interface, so anything that implements this interface can be a member of this array. For this code to work both Developer and Student need to implement People interface either directly or through the embedded Person struct.
– Mad Wombat
Mar 28 at 15:20
1
1
But
Peoplealex, john, jitesh on its own is not valid. It needs [...] to make it an array of People. Or simply [] to make it a slice of People.– Mad Wombat
Mar 28 at 15:21
But
Peoplealex, john, jitesh on its own is not valid. It needs [...] to make it an array of People. Or simply [] to make it a slice of People.– Mad Wombat
Mar 28 at 15:21
[...]People is an array. []People is a slice. These are two different types.– Mad Wombat
Mar 28 at 15:23
[...]People is an array. []People is a slice. These are two different types.– Mad Wombat
Mar 28 at 15:23
[...]People would create an empty array of zero elements, but [...]Peoplealex, john, jitesh would create a populated array of three elements– Mad Wombat
Mar 28 at 15:24
[...]People would create an empty array of zero elements, but [...]Peoplealex, john, jitesh would create a populated array of three elements– Mad Wombat
Mar 28 at 15:24
add a comment
|
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
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%2f55399823%2fconverting-a-type-to-an-array-go-syntax%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
4
It's all here: golang.org/ref/spec. 1) A composite literal, it is syntax, not "paradigm". 2) Create an array whose length is the same as the number of elements provided to the literal.
– mkopriva
Mar 28 at 14:17
@mkopriva golang.org/search?q=%5B...%5D
– overexchange
Mar 28 at 14:17
2
1, 2, 3is a composite literal, and "The notation...specifies an array length equal to the maximum element index plus one."– icza
Mar 28 at 14:18
1
@overexchange what are you implying with that link? That it's hard to find? Try the link in my first comment and try searching that page with CTRL/CMD+F, that's more efficient.
– mkopriva
Mar 28 at 14:20
1
Unnamed arrays* can't implement interfaces. It's objects that implement interfaces, so you have an array of objects and those objects implement the interface. Edit: I use objects here, but it may be more correct to say structs.
– Devon
Mar 28 at 14:31