Delete an element in slice using append in GoLang

2018-04-15 09:07:44
Memory, Renee
来源:
吕滔博客
翻译 95


Array [a b c]

Task: Delete the second element. Get [a c].

Steps:


package main

import (
    "fmt"
)

//function used to delete the element
func remove(s []string, i int) []string {
    return append(s[:i], s[i+1:]...)
}

func main() {
    s := []string{"a", "b", "c"}
    fmt.Println(s)
    s = remove(s, 1)
    fmt.Println(s)
}

    发表评论
    评论通过审核后显示。