How to get rid of zero value in a int slice in Go?
I am trying to find even numbers in a list of numbers, here is my attempt:
package main
import "fmt"
func main() {
nums := int{1, 2, 3, 4, 5, 6, 7}
res := int{}
for n := range nums {
if n%2 == 0 {
res = append(res, n)
}
}
fmt.Println(res)
}
It seems straightforward; however, when I run the program, I got the result
[0 2 4 6]
Where does the zero come from? It must be from the empty slice res
. How can I get rid of this zero?
for-loop go slice
add a comment |
I am trying to find even numbers in a list of numbers, here is my attempt:
package main
import "fmt"
func main() {
nums := int{1, 2, 3, 4, 5, 6, 7}
res := int{}
for n := range nums {
if n%2 == 0 {
res = append(res, n)
}
}
fmt.Println(res)
}
It seems straightforward; however, when I run the program, I got the result
[0 2 4 6]
Where does the zero come from? It must be from the empty slice res
. How can I get rid of this zero?
for-loop go slice
1
This is covered in the Tour of Go.
– Adrian
Nov 19 '18 at 14:14
add a comment |
I am trying to find even numbers in a list of numbers, here is my attempt:
package main
import "fmt"
func main() {
nums := int{1, 2, 3, 4, 5, 6, 7}
res := int{}
for n := range nums {
if n%2 == 0 {
res = append(res, n)
}
}
fmt.Println(res)
}
It seems straightforward; however, when I run the program, I got the result
[0 2 4 6]
Where does the zero come from? It must be from the empty slice res
. How can I get rid of this zero?
for-loop go slice
I am trying to find even numbers in a list of numbers, here is my attempt:
package main
import "fmt"
func main() {
nums := int{1, 2, 3, 4, 5, 6, 7}
res := int{}
for n := range nums {
if n%2 == 0 {
res = append(res, n)
}
}
fmt.Println(res)
}
It seems straightforward; however, when I run the program, I got the result
[0 2 4 6]
Where does the zero come from? It must be from the empty slice res
. How can I get rid of this zero?
for-loop go slice
for-loop go slice
edited Nov 19 '18 at 12:50
icza
167k25333366
167k25333366
asked Nov 19 '18 at 12:43
NickNick
2,46942758
2,46942758
1
This is covered in the Tour of Go.
– Adrian
Nov 19 '18 at 14:14
add a comment |
1
This is covered in the Tour of Go.
– Adrian
Nov 19 '18 at 14:14
1
1
This is covered in the Tour of Go.
– Adrian
Nov 19 '18 at 14:14
This is covered in the Tour of Go.
– Adrian
Nov 19 '18 at 14:14
add a comment |
1 Answer
1
active
oldest
votes
for n := range nums {
// ...
}
n
is not the elements of the nums
slice, it is the index. So basically you tested and added the indices of the elements to your res
result slice.
Instead do this:
for _, n := range nums {
// ...
}
With this change, output will be (try it on the Go Playground):
[2 4 6]
This is detailed in Spec: For statements, For statements with range clause:
For each iteration, iteration values are produced as follows if the respective iteration variables are present:
Range expression 1st value 2nd value
array or slice a [n]E, *[n]E, or E index i int a[i] E
string s string type index i int see below rune
map m map[K]V key k K m[k] V
channel c chan E, <-chan E element e E
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%2f53374915%2fhow-to-get-rid-of-zero-value-in-a-int-slice-in-go%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
for n := range nums {
// ...
}
n
is not the elements of the nums
slice, it is the index. So basically you tested and added the indices of the elements to your res
result slice.
Instead do this:
for _, n := range nums {
// ...
}
With this change, output will be (try it on the Go Playground):
[2 4 6]
This is detailed in Spec: For statements, For statements with range clause:
For each iteration, iteration values are produced as follows if the respective iteration variables are present:
Range expression 1st value 2nd value
array or slice a [n]E, *[n]E, or E index i int a[i] E
string s string type index i int see below rune
map m map[K]V key k K m[k] V
channel c chan E, <-chan E element e E
add a comment |
for n := range nums {
// ...
}
n
is not the elements of the nums
slice, it is the index. So basically you tested and added the indices of the elements to your res
result slice.
Instead do this:
for _, n := range nums {
// ...
}
With this change, output will be (try it on the Go Playground):
[2 4 6]
This is detailed in Spec: For statements, For statements with range clause:
For each iteration, iteration values are produced as follows if the respective iteration variables are present:
Range expression 1st value 2nd value
array or slice a [n]E, *[n]E, or E index i int a[i] E
string s string type index i int see below rune
map m map[K]V key k K m[k] V
channel c chan E, <-chan E element e E
add a comment |
for n := range nums {
// ...
}
n
is not the elements of the nums
slice, it is the index. So basically you tested and added the indices of the elements to your res
result slice.
Instead do this:
for _, n := range nums {
// ...
}
With this change, output will be (try it on the Go Playground):
[2 4 6]
This is detailed in Spec: For statements, For statements with range clause:
For each iteration, iteration values are produced as follows if the respective iteration variables are present:
Range expression 1st value 2nd value
array or slice a [n]E, *[n]E, or E index i int a[i] E
string s string type index i int see below rune
map m map[K]V key k K m[k] V
channel c chan E, <-chan E element e E
for n := range nums {
// ...
}
n
is not the elements of the nums
slice, it is the index. So basically you tested and added the indices of the elements to your res
result slice.
Instead do this:
for _, n := range nums {
// ...
}
With this change, output will be (try it on the Go Playground):
[2 4 6]
This is detailed in Spec: For statements, For statements with range clause:
For each iteration, iteration values are produced as follows if the respective iteration variables are present:
Range expression 1st value 2nd value
array or slice a [n]E, *[n]E, or E index i int a[i] E
string s string type index i int see below rune
map m map[K]V key k K m[k] V
channel c chan E, <-chan E element e E
edited Nov 19 '18 at 17:35
answered Nov 19 '18 at 12:45
iczaicza
167k25333366
167k25333366
add a comment |
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%2f53374915%2fhow-to-get-rid-of-zero-value-in-a-int-slice-in-go%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
1
This is covered in the Tour of Go.
– Adrian
Nov 19 '18 at 14:14