いろいろ備忘録日記

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

Goメモ-166 (stringsとbytesパッケージにCut関数が追加, Go 1.18)

概要

以下忘れないうちにメモメモ。。。

Go 1.18 で strings と bytes パッケージに Cut という関数が追加されるみたいですね。

github.com

痒いところに手が届く感じの関数が追加されるのはすごく助かりますね。

Go 1.18 から存在なのを確認

$ docker image pull golang:1.17-bullseye
$ docker container run -it --rm golang:1.17-bullseye

root@5dd52caaaa2b:/go# go version
go version go1.17.5 linux/amd64
root@5dd52caaaa2b:/go# go doc strings.Cut
doc: no symbol Cut in package strings
exit status 1

Go 1.17には存在しない。

$ docker image pull golang:1.18beta1-bullseye
$ docker container run -it --rm golang:1.18beta1-bullseye

root@846051cd9faa:/go# go version
go version go1.18beta1 linux/amd64
root@846051cd9faa:/go# go doc strings.Cut
package strings // import "strings"

func Cut(s, sep string) (before, after string, found bool)
    Cut slices s around the first instance of sep, returning the text before and
    after sep. The found result reports whether sep appears in s. If sep does
    not appear in s, cut returns s, "", false.

Go 1.18には存在する。

試してみる

環境は先日の記事

devlights.hatenablog.com

の環境です。

package main

import (
        "bytes"
        "fmt"
        "strings"
        "testing"
)

func TestStringsCut(t *testing.T) {
        tests := []struct {
                input, sep, before, after string
                found                     bool
        }{
                {"hello", "e", "h", "llo", true},
                {"hello", "z", "hello", "", false},
        }

        for _, test := range tests {
                test := test
                title := fmt.Sprintf("%s[sep:%s]", test.input, test.sep)
                t.Run(title, func(t *testing.T) {
                        before, after, found := strings.Cut(test.input, test.sep)

                        if test.found != found {
                                t.Errorf("found:[want] %v\t[got] %v", test.found, found)
                        }

                        if test.before != before {
                                t.Errorf("before:[want] %v\t[got] %v", test.before, before)
                        }

                        if test.after != after {
                                t.Errorf("after:[want] %v\t[got] %v", test.after, after)
                        }
                })
        }
}

func TestBytesCut(t *testing.T) {
        tests := []struct {
                input, sep, before, after []byte
                found                     bool
        }{
                {[]byte{1, 2, 3}, []byte{2}, []byte{1}, []byte{3}, true},
                {[]byte{1, 2, 3}, []byte{4}, []byte{1, 2, 3}, []byte{}, false},
        }

        for _, test := range tests {
                test := test
                title := fmt.Sprintf("%s[sep:%s]", test.input, test.sep)
                t.Run(title, func(t *testing.T) {
                        before, after, found := bytes.Cut(test.input, test.sep)

                        if test.found != found {
                                t.Errorf("found:[want] %v\t[got] %v", test.found, found)
                        }

                        if bytes.Compare(test.before, before) != 0 {
                                t.Errorf("before:[want] %v\t[got] %v", test.before, before)
                        }

                        if bytes.Compare(test.after, after) != 0 {
                                t.Errorf("after:[want] %v\t[got] %v", test.after, after)
                        }
                })
        }
}

試してみましょう。

dev@4b0c889aa3de:/workspace$ go version
go version go1.18beta1 linux/amd64

dev@4b0c889aa3de:/workspace$ go test -v main_test.go
=== RUN   TestStringsCut
=== RUN   TestStringsCut/hello[sep:e]
=== RUN   TestStringsCut/hello[sep:z]
--- PASS: TestStringsCut (0.00s)
    --- PASS: TestStringsCut/hello[sep:e] (0.00s)
    --- PASS: TestStringsCut/hello[sep:z] (0.00s)
=== RUN   TestBytesCut
=== RUN   TestBytesCut/\x01\x02\x03[sep:\x02]
=== RUN   TestBytesCut/\x01\x02\x03[sep:\x04]
--- PASS: TestBytesCut (0.00s)
    --- PASS: TestBytesCut/\x01\x02\x03[sep:\x02] (0.00s)
    --- PASS: TestBytesCut/\x01\x02\x03[sep:\x04] (0.00s)
PASS
ok      command-line-arguments  0.002s

参考情報

go.dev


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

  • いろいろ備忘録日記まとめ

devlights.github.io

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

  • いろいろ備忘録日記サンプルソース置き場

github.com

github.com

github.com