“Usuń z plasterek” Kod odpowiedzi

Jak usunąć element z tablicy podkładowej Golang

package main

import (
    "fmt"
)

func RemoveIndex(s []int, index int) []int {
    ret := make([]int, 0)
    ret = append(ret, s[:index]...)
    return append(ret, s[index+1:]...)
}

func main() {
    all := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
    fmt.Println("all: ", all) //[0 1 2 3 4 5 6 7 8 9]
    removeIndex := RemoveIndex(all, 5)

    fmt.Println("all: ", all) //[0 1 2 3 4 5 6 7 8 9]
    fmt.Println("removeIndex: ", removeIndex) //[0 1 2 3 4 6 7 8 9]

    removeIndex[0] = 999
    fmt.Println("all: ", all) //[0 1 2 3 4 5 6 7 9 9]
    fmt.Println("removeIndex: ", removeIndex) //[999 1 2 3 4 6 7 8 9]
}
Kind Kouprey

Usuń z plasterek

func remove(slice []int, s int) []int {
    return append(slice[:s], slice[s+1:]...)
}
Navid2zp

Odpowiedzi podobne do “Usuń z plasterek”

Pytania podobne do “Usuń z plasterek”

Więcej pokrewnych odpowiedzi na “Usuń z plasterek” w Go

Przeglądaj popularne odpowiedzi na kod według języka

Przeglądaj inne języki kodu