FAQ
I'm starting to learn go. I'm looking to remove an element from a slice.
Below I wrote a small test function to understand how it works. However I
keep receiving a build error "cannot use _tmpSlice (type []int) as type int
in append". I don't quite understand the error, it seems like the
slice[x:y] notation returns a slice and the append function combines slices
with element or other slices.

Thanks in advance


package main

import (
"fmt"
"testing"
)

func TestGetTemplateByName(t *testing.T) {

testSlice := []int{1, 2, 3, 4, 5}
_tmpSlice := make([]int, 0)

for k, num := range testSlice {
fmt.Println(k, " -> ", num, testSlice[:k])

if num == 3 {
//copy(_tmpSlice, testSlice[k+1:])
_tmpSlice = testSlice[k+1:]
fmt.Println(_tmpSlice, k+1, testSlice[:k], testSlice[k+1:])
//testSlice = append(testSlice[:k], _tmpSlice)
}
}

}

------------ Go Test -v

% go test -v


=== RUN TestGetTemplateByName
0 -> 1 []
1 -> 2 [1]
2 -> 3 [1 2]
[4 5] 3 [4 5] [1 2]
3 -> 4 [1 2 3]
4 -> 5 [1 2 3 4]
--- PASS: TestGetTemplateByName (0.00 seconds)
main_test.go:27: [1 2 3 4 5]
PASS
ok com.cassbeats/leonardoTemplateBuilder/src 0.071s

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Minux at Feb 19, 2014 at 6:22 pm

    On Feb 19, 2014 1:06 PM, "Alex Casanova" wrote:
    I'm starting to learn go. I'm looking to remove an element from a slice.
    Below I wrote a small test function to understand how it works. However I
    keep receiving a build error "cannot use _tmpSlice (type []int) as type int
    in append". I don't quite understand the error, it seems like the
    slice[x:y] notation returns a slice and the append function combines slices
    with element or other slices.
    Thanks in advance


    package main

    import (
    "fmt"
    "testing"
    )

    func TestGetTemplateByName(t *testing.T) {

    testSlice := []int{1, 2, 3, 4, 5}
    _tmpSlice := make([]int, 0)

    for k, num := range testSlice {
    fmt.Println(k, " -> ", num, testSlice[:k])

    if num == 3 {
    //copy(_tmpSlice, testSlice[k+1:])
    _tmpSlice = testSlice[k+1:]
    fmt.Println(_tmpSlice, k+1, testSlice[:k], testSlice[k+1:])
    //testSlice = append(testSlice[:k], _tmpSlice)
    }
    }

    }

    ------------ Go Test -v

    % go test -v
    === RUN TestGetTemplateByName
    0 -> 1 []
    1 -> 2 [1]
    2 -> 3 [1 2]
    [4 5] 3 [4 5] [1 2]
    3 -> 4 [1 2 3]
    4 -> 5 [1 2 3 4]
    --- PASS: TestGetTemplateByName (0.00 seconds)
    main_test.go:27: [1 2 3 4 5]
    PASS
    ok com.cassbeats/leonardoTemplateBuilder/src 0.071s
    please take a look at https://code.google.com/p/go-wiki/wiki/SliceTricks

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Matthew Kane at Feb 19, 2014 at 6:44 pm

    On Wed, Feb 19, 2014 at 1:06 PM, Alex Casanova wrote:
    I'm starting to learn go. I'm looking to remove an element from a slice.
    Below I wrote a small test function to understand how it works. However I
    keep receiving a build error "cannot use _tmpSlice (type []int) as type int
    in append". I don't quite understand the error, it seems like the slice[x:y]
    notation returns a slice and the append function combines slices with
    element or other slices.
    You are close. See the spec:
    http://golang.org/ref/spec#Appending_and_copying_slices

    append takes one or more arguments that are the element type of the
    slice in the first argument. If you want to append a slice to another
    slice, you can add three dots after the slice.

    testSlice = append(testSlice[:k], _tmpSlice…)

    Also, if you don't care about the order, you can swap the last element
    of the slice with the element you want to delete, then shorten the
    slice by 1.

    https://code.google.com/p/go-wiki/wiki/SliceTricks

    Thanks in advance


    package main

    import (
    "fmt"
    "testing"
    )

    func TestGetTemplateByName(t *testing.T) {

    testSlice := []int{1, 2, 3, 4, 5}
    _tmpSlice := make([]int, 0)

    for k, num := range testSlice {
    fmt.Println(k, " -> ", num, testSlice[:k])

    if num == 3 {
    //copy(_tmpSlice, testSlice[k+1:])
    _tmpSlice = testSlice[k+1:]
    fmt.Println(_tmpSlice, k+1, testSlice[:k], testSlice[k+1:])
    //testSlice = append(testSlice[:k], _tmpSlice)
    }
    }

    }

    ------------ Go Test -v

    % go test -v
    === RUN TestGetTemplateByName
    0 -> 1 []
    1 -> 2 [1]
    2 -> 3 [1 2]
    [4 5] 3 [4 5] [1 2]
    3 -> 4 [1 2 3]
    4 -> 5 [1 2 3 4]
    --- PASS: TestGetTemplateByName (0.00 seconds)
    main_test.go:27: [1 2 3 4 5]
    PASS
    ok com.cassbeats/leonardoTemplateBuilder/src 0.071s

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.


    --
    matt kane
    twitter: the_real_mkb / nynexrepublic
    http://hydrogenproject.com

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Alex Casanova at Feb 19, 2014 at 8:28 pm
    Thanks alot guys, it now works
    On Wednesday, February 19, 2014 1:06:21 PM UTC-5, Alex Casanova wrote:

    I'm starting to learn go. I'm looking to remove an element from a slice.
    Below I wrote a small test function to understand how it works. However I
    keep receiving a build error "cannot use _tmpSlice (type []int) as type int
    in append". I don't quite understand the error, it seems like the
    slice[x:y] notation returns a slice and the append function combines slices
    with element or other slices.

    Thanks in advance


    package main

    import (
    "fmt"
    "testing"
    )

    func TestGetTemplateByName(t *testing.T) {

    testSlice := []int{1, 2, 3, 4, 5}
    _tmpSlice := make([]int, 0)

    for k, num := range testSlice {
    fmt.Println(k, " -> ", num, testSlice[:k])

    if num == 3 {
    //copy(_tmpSlice, testSlice[k+1:])
    _tmpSlice = testSlice[k+1:]
    fmt.Println(_tmpSlice, k+1, testSlice[:k], testSlice[k+1:])
    //testSlice = append(testSlice[:k], _tmpSlice)
    }
    }

    }

    ------------ Go Test -v

    % go test -v


    === RUN TestGetTemplateByName
    0 -> 1 []
    1 -> 2 [1]
    2 -> 3 [1 2]
    [4 5] 3 [4 5] [1 2]
    3 -> 4 [1 2 3]
    4 -> 5 [1 2 3 4]
    --- PASS: TestGetTemplateByName (0.00 seconds)
    main_test.go:27: [1 2 3 4 5]
    PASS
    ok com.cassbeats/leonardoTemplateBuilder/src 0.071s
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 19, '14 at 6:06p
activeFeb 19, '14 at 8:28p
posts4
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase