Skip to content

Commit 525efaa

Browse files
committed
Add embed_string.go
1 parent 88cb912 commit 525efaa

File tree

6 files changed

+55
-0
lines changed

6 files changed

+55
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# サンプルリスト
2+
3+
このディレクトリには以下のサンプルがあります。
4+
5+
|file|example name|note|
6+
|----|------------|----|
7+
|embed\_string.go|embed\_string|iota の基本的な使い方のサンプルです.|
8+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
Package embeds -- embed パッケージについてのサンプルが配置されているパッケージです。
3+
*/
4+
package embeds
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package embeds
2+
3+
import (
4+
_ "embed" // Go1.16 の embed パッケージを利用するために必要な import
5+
6+
"github.com/devlights/gomy/output"
7+
)
8+
9+
//go:embed helloworld.txt
10+
var s string // helloworld.txt の中身が string として設定される
11+
12+
// EmbedString は、embed パッケージの機能を確認するサンプルです (文字列として値を取得)
13+
func EmbedString() error {
14+
//
15+
// go:embed で指定されている部分は、コンパイル時に埋め込みが施される
16+
// なので、プログラム実行時には既に値は変数に設定済みとなっている.
17+
//
18+
output.Stdoutl("[helloworld.txt]", s)
19+
return nil
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package embeds
2+
3+
import (
4+
"github.com/devlights/try-golang/pkg/mappings"
5+
)
6+
7+
type (
8+
register struct{}
9+
)
10+
11+
// NewRegister -- このパッケージ用のサンプルを登録する mappings.Register を生成します。
12+
func NewRegister() mappings.Register {
13+
return new(register)
14+
}
15+
16+
// Regist -- 登録します.
17+
func (r *register) Regist(m mappings.ExampleMapping) {
18+
m["embed_string"] = EmbedString
19+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
hello
2+
world

internal/examples/basic/examples.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/devlights/try-golang/internal/examples/basic/convert"
1313
"github.com/devlights/try-golang/internal/examples/basic/cryptos"
1414
"github.com/devlights/try-golang/internal/examples/basic/defers"
15+
"github.com/devlights/try-golang/internal/examples/basic/embeds"
1516
"github.com/devlights/try-golang/internal/examples/basic/enum"
1617
"github.com/devlights/try-golang/internal/examples/basic/errs"
1718
"github.com/devlights/try-golang/internal/examples/basic/fileio"
@@ -72,6 +73,7 @@ func (r *register) Regist(m mappings.ExampleMapping) {
7273
convert.NewRegister().Regist(m)
7374
cryptos.NewRegister().Regist(m)
7475
defers.NewRegister().Regist(m)
76+
embeds.NewRegister().Regist(m)
7577
enum.NewRegister().Regist(m)
7678
errs.NewRegister().Regist(m)
7779
fileio.NewRegister().Regist(m)

0 commit comments

Comments
 (0)