Home Backend Development Golang How to delete slices in go language

How to delete slices in go language

Dec 30, 2020 pm 05:50 PM
go language

go languageMethod to delete slices: First create a go sample file; then declare variables; finally pass "ss=append(ss[:index],ss[index 1:]. ..)print("after delete",ss)" can be used to delete slice elements.

How to delete slices in go language

The operating environment of this tutorial: Windows 7 system, Go version 1.14, Dell G3 computer.

1. Declare variables, go automatically initializes to nil, length: 0, address: 0, nil

func main(){
    var ss []string;
    fmt.Printf("length:%v \taddr:%p \tisnil:%v",len(ss),ss, ss==nil)    
}
 
---
Running...
 
length:0     addr:0x0     isnil:true
Success: process exited with code 0.
Copy after login

2. Add, delete, insert operations of slices

func main(){
    var ss []string;
    fmt.Printf("[ local print ]\t:\t length:%v\taddr:%p\tisnil:%v\n",len(ss),ss, ss==nil)    
    print("func print",ss)
    //切片尾部追加元素append elemnt
    for i:=0;i<10;i++{
        ss=append(ss,fmt.Sprintf("s%d",i));
    }
    fmt.Printf("[ local print ]\t:\tlength:%v\taddr:%p\tisnil:%v\n",len(ss),ss, ss==nil)    
    print("after append",ss)
    //删除切片元素remove element at index
    index:=5;
    ss=append(ss[:index],ss[index+1:]...)
    print("after delete",ss)
    //在切片中间插入元素insert element at index;
    //注意:保存后部剩余元素,必须新建一个临时切片
    rear:=append([]string{},ss[index:]...) 
    ss=append(ss[0:index],"inserted")
    ss=append(ss,rear...)
    print("after insert",ss)
}
func print(msg string,ss []string){
    fmt.Printf("[ %20s ]\t:\tlength:%v\taddr:%p\tisnil:%v\tcontent:%v",msg,len(ss),ss, ss==nil,ss)    
    fmt.Println()
}
------
Running...
 
[ local print ]    :     length:0    addr:0x0    isnil:true
[           func print ]    :    length:0    addr:0x0    isnil:true    content:[]
[ local print ]    :    length:10    addr:0xc208056000    isnil:false
[         after append ]    :    length:10    addr:0xc208056000    isnil:false    content:[s0 s1 s2 s3 s4 s5 s6 s7 s8 s9]
[         after delete ]    :    length:9    addr:0xc208056000    isnil:false    content:[s0 s1 s2 s3 s4 s6 s7 s8 s9]
[         after insert ]    :    length:10    addr:0xc208056000    isnil:false    content:[s0 s1 s2 s3 s4 inserted s6 s7 s8 s9]
 
Success: process exited with code 0.
Copy after login

Recommended: "go Language Tutorial"

The above is the detailed content of How to delete slices in go language. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

In Go, why does printing strings with Println and string() functions have different effects? In Go, why does printing strings with Println and string() functions have different effects? Apr 02, 2025 pm 02:03 PM

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

What should I do if the custom structure labels in GoLand are not displayed? What should I do if the custom structure labels in GoLand are not displayed? Apr 02, 2025 pm 05:09 PM

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

What is the difference between `var` and `type` keyword definition structure in Go language? What is the difference between `var` and `type` keyword definition structure in Go language? Apr 02, 2025 pm 12:57 PM

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

Which libraries in Go are developed by large companies or provided by well-known open source projects? Which libraries in Go are developed by large companies or provided by well-known open source projects? Apr 02, 2025 pm 04:12 PM

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

Why is it necessary to pass pointers when using Go and viper libraries? Why is it necessary to pass pointers when using Go and viper libraries? Apr 02, 2025 pm 04:00 PM

Go pointer syntax and addressing problems in the use of viper library When programming in Go language, it is crucial to understand the syntax and usage of pointers, especially in...

See all articles