「Go言語」delete()で配列の要素を削除するサンプル

2021年2月10日

書式
delete(配列名,"キー名")
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package main
import "fmt"
func main() {
cft := map[string]int{"key01": 801, "key02": 802, "key03": 803, "key04": 804}
fmt.Println("削除前")
fmt.Println(cft)
delete(cft, "key02")
delete(cft, "key04")
fmt.Println("削除後")
fmt.Println(cft)
}
package main import "fmt" func main() { cft := map[string]int{"key01": 801, "key02": 802, "key03": 803, "key04": 804} fmt.Println("削除前") fmt.Println(cft) delete(cft, "key02") delete(cft, "key04") fmt.Println("削除後") fmt.Println(cft) }
package main
 
import "fmt"

func main() {
 
cft := map[string]int{"key01": 801, "key02": 802, "key03": 803, "key04": 804}
 	fmt.Println("削除前")
      fmt.Println(cft)
      
      delete(cft, "key02")
      
      delete(cft, "key04")
  
    fmt.Println("削除後")
      fmt.Println(cft)
 
}

実行結果
>go run sample02.go
削除前
map[key01:801 key02:802 key03:803 key04:804]
削除後
map[key01:801 key03:803]

Go言語

Posted by arkgame