いろいろ備忘録日記

主に .NET とか Go とか Flutter とか Python絡みのメモを公開しています。

Goメモ-320 (Go 1.21 で clear ビルドイン関数が追加される)

関連記事

Goメモ-177 (リリース前の機能も含めて最新のGoを試す)(gotip, gccない環境でインストール) - いろいろ備忘録日記

GitHub - devlights/blog-summary: ブログ「いろいろ備忘録日記」のまとめ

概要

以下、自分用のメモです。忘れないうちにメモメモ。。。

以下で知りました。

github.com

Acceptされているので、入ることは確定みたいですね。

試してみる

gotipを使って試してみます。以下、Gitpod上で試しました。

まずはgotipをダウンロード。

$ lsb_relase -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.2 LTS
Release:        22.04
Codename:       jammy


$ go install golang.org/dl/gotip@latest

$ gotip download
Cloning into '/home/gitpod/sdk/gotip'...
remote: Finding sources: 100% (13479/13479)
remote: Total 13479 (delta 1807), reused 8989 (delta 1807)
Receiving objects: 100% (13479/13479), 27.29 MiB | 5.18 MiB/s, done.
Resolving deltas: 100% (1807/1807), done.
Updating files: 100% (12407/12407), done.
Updating the go development tree...
From https://go.googlesource.com/go
 * branch            master     -> FETCH_HEAD
HEAD is now at e4e8f9b cmd/go: clear GOOS environment variable in TestScript/env_write
Building Go cmd/dist using /home/gitpod/go. (go1.20.4 linux/amd64)
Building Go toolchain1 using /home/gitpod/go.
Building Go bootstrap cmd/go (go_bootstrap) using Go toolchain1.
Building Go toolchain2 using go_bootstrap and Go toolchain1.
Building Go toolchain3 using go_bootstrap and Go toolchain2.
Building packages and commands for linux/amd64.
---
Installed Go for linux/amd64 in /home/gitpod/sdk/gotip
Installed commands in /home/gitpod/sdk/gotip/bin
*** You need to add /home/gitpod/sdk/gotip/bin to your PATH.
Success. You may now run 'gotip'!


$ gotip version
go version devel go1.21-e4e8f9b Tue May 23 04:38:43 2023 +0000 linux/amd64

オッケイ。てことで、clearビルドイン関数があるかを go doc で見てみます。

$ gotip doc builtin.clear
package builtin // import "builtin"

func clear[T ~[]Type | ~map[Type]Type1](t T)
    The clear built-in function clears maps and slices. For maps, clear deletes
    all entries, resulting in an empty map. For slices, clear sets all elements
    up to the length of the slice to the zero value of the respective element
    type. If the argument type is a type parameter, the type parameter's type
    set must contain only map or slice types, and clear performs the operation
    implied by the type argument.

ちゃんとありますね。

軽くだけ使ってみたサンプル

package main

import "fmt"

func main() {
    if err := run(); err != nil {
        panic(err)
    }
}

func run() error {
    //
    // Go 1.21 で clearビルドイン関数 が追加
    //
    var (
        s = []int{1, 2, 3}
        m = map[int]bool{1: true, 2: false}
    )

    fmt.Println(s, m)

    clear(s)
    clear(m)

    fmt.Println(s, m)

    return nil
}

まず、go 1.20 で試してみましょう。

$ go version
go version go1.20.4 linux/amd64

$ go run app/main.go
# command-line-arguments
app/main.go:11:6: internal compiler error: panic: interface conversion: interface is nil, not ir.Node

Please file a bug report including a short program that triggers the error.
https://go.dev/issue/new

当たり前ですが、エラーになります。

てことで、gotip側でやってみましょう。

$ gotip run main.go
[1 2 3] map[1:true 2:false]
[0 0 0] map[]

動きますね。

マップはキーも含めてクリアされるけど、スライスは要素をゼロ値にする感じ。スライスの場合は要素数は変わらない。

参考情報

Goのおすすめ書籍

Go言語による並行処理

Go言語による並行処理

Amazon


過去の記事については、以下のページからご参照下さい。

サンプルコードは、以下の場所で公開しています。