File tree Expand file tree Collapse file tree 4 files changed +102
-4
lines changed
Expand file tree Collapse file tree 4 files changed +102
-4
lines changed Original file line number Diff line number Diff line change 22
33このディレクトリには以下のサンプルがあります。
44
5- | file | example name | note |
6- | ---------------- | ------------- | --------------------------------------- |
7- | unsafe_sizeof.go | unsafe_sizeof | unsafe.Sizeof() についてのサンプルです. |
8-
5+ | file | example name | note |
6+ | -------------------- | ----------------- | --------------------------------------- |
7+ | unsafe_sizeof.go | unsafe_sizeof | unsafe.Sizeof() についてのサンプルです. |
8+ | unsafe_string.go | unsafe_string | unsafe.String() のサンプルです. |
9+ | unsafe_stringdata.go | unsafe_stringdata | unsafe.StringData() のサンプルです. |
Original file line number Diff line number Diff line change @@ -14,4 +14,6 @@ func NewRegister() mapping.Register {
1414// Regist -- 登録します.
1515func (r * register ) Regist (m mapping.ExampleMapping ) {
1616 m ["unsafe_sizeof" ] = Sizeof
17+ m ["unsafe_string" ] = UnsafeString
18+ m ["unsafe_stringdata" ] = UnsafeStringData
1719}
Original file line number Diff line number Diff line change 1+ package unsafes
2+
3+ import (
4+ "syscall"
5+ "unsafe"
6+
7+ "github.com/devlights/gomy/output"
8+ )
9+
10+ // UnsafeString は、unsafe.String() のサンプルです.
11+ //
12+ // # REFERENCES
13+ //
14+ // - https://pkg.go.dev/unsafe@go1.22.2#String
15+ // - https://mattn.kaoriya.net/software/lang/go/20220907112622.htm
16+ func UnsafeString () error {
17+ const str = "hello world"
18+
19+ //
20+ // *byte を作成
21+ //
22+ var (
23+ buf * byte
24+ err error
25+ )
26+
27+ buf , err = syscall .BytePtrFromString (str )
28+ if err != nil {
29+ return err
30+ }
31+
32+ //
33+ // unsafe.String() で、Goのstring型に変換
34+ //
35+ str2 := unsafe .String (buf , len (str ))
36+
37+ output .Stdoutl ("[str ]" , str )
38+ output .Stdoutl ("[str2]" , str2 )
39+
40+ return nil
41+
42+ /*
43+ $ task
44+ task: [build] go build .
45+ task: [run] ./try-golang -onetime
46+
47+ ENTER EXAMPLE NAME: unsafe_string
48+
49+ [Name] "unsafe_string"
50+ [str ] hello world
51+ [str2] hello world
52+
53+
54+ [Elapsed] 12.471µs
55+ */
56+
57+ }
Original file line number Diff line number Diff line change 1+ package unsafes
2+
3+ import (
4+ "unsafe"
5+
6+ "github.com/devlights/gomy/output"
7+ )
8+
9+ // UnsafeStringData は、unsafe.StringData() のサンプルです.
10+ //
11+ // # REFERENCES
12+ // - https://pkg.go.dev/unsafe@go1.22.2#StringData
13+ // - https://mattn.kaoriya.net/software/lang/go/20220907112622.htm
14+ func UnsafeStringData () error {
15+ const str = "hello world"
16+
17+ buf := unsafe .StringData (str )
18+ str2 := unsafe .String (buf , len (str ))
19+
20+ output .Stdoutl ("[str2]" , str2 )
21+
22+ return nil
23+
24+ /*
25+ $ task
26+ task: [build] go build .
27+ task: [run] ./try-golang -onetime
28+
29+ ENTER EXAMPLE NAME: unsafe_stringdata
30+
31+ [Name] "unsafe_stringdata"
32+ [str2] hello world
33+
34+
35+ [Elapsed] 10.99µs
36+ */
37+
38+ }
You can’t perform that action at this time.
0 commit comments