18
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

sliceからN番目の要素を削除する

Posted at

簡単だけどすぐ忘れそう。

package main

import (
	"fmt"
)

func main() {
	t := []string{"0番目","1番目","2番目","3番目","4番目"}
	fmt.Println(t)
	n := 2
	fmt.Printf("%d番目を削除\n", n)
	t = unset(t, n)
	fmt.Println(t)
}

func unset(s []string, i int) []string {
	if i >= len(s) {
		return s
	}
	return append(s[:i], s[i+1:]...)
}

18
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?