Delete an element in slice using append in GoLang

2018-04-12 09:43:00
Memory, Renee
Original
330


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)
}


Done!

Write a Comment
Comment will be posted once reviewed.
blog-why

Why choose ZenTao

Leading

Serving 30,000+ teams

Serving 200,000+ projects

Serving 800,000+ developers

The #1 in local market share

Free & Open

Open source and unlimited to commercial

Powerful extension mechanism and various plug-ins

Available on Github

Either Self-Host or Cloud Apps

Professional

Refined ALM support

Zero downtime upgrades 

Integrate Git and SVN (pro)

Word and Excel import & export (pro)

Preview-Edit-Diff document online(ent)

Integrate OPS-Attendance-Feedback(ent)

Guaranteed

ZenTao team has involved in open source since 2004

Frequent releases and free upgrade forever(even self-hosted) 

Instant and powerful support for 20000+ companies

Easy

Out of the box, Less config

Nice price for small team($9.9)

Design to adhere Scrum Best practice

For Agile but not restrict to Agile

Flexible

Applicable for different sized teams

Applicable for Agile/Waterfall

Modules can be used in any combination

Convenient customization