Skip to content

Commit 20963bb

Browse files
committed
Add examples/basic/formatting directory & Add example
1 parent 27a7047 commit 20963bb

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

examples/basic/examples.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/devlights/try-golang/examples/basic/errs"
1414
"github.com/devlights/try-golang/examples/basic/fileio"
1515
"github.com/devlights/try-golang/examples/basic/filepaths"
16+
"github.com/devlights/try-golang/examples/basic/formatting"
1617
"github.com/devlights/try-golang/examples/basic/functions"
1718
"github.com/devlights/try-golang/examples/basic/helloworld"
1819
"github.com/devlights/try-golang/examples/basic/ifs"
@@ -65,6 +66,7 @@ func (r *register) Regist(m mappings.ExampleMapping) {
6566
errs.NewRegister().Regist(m)
6667
fileio.NewRegister().Regist(m)
6768
filepaths.NewRegister().Regist(m)
69+
formatting.NewRegister().Regist(m)
6870
functions.NewRegister().Regist(m)
6971
helloworld.NewRegister().Regist(m)
7072
ifs.NewRegister().Regist(m)
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+
|formatting\_adverb\_explicit\_argument\_indexes.go|formatting\_adverb\_explicit\_argument\_indexes|フォーマッティングの Explicit argument indexes についてのサンプルです。|
8+

examples/basic/formatting/doc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/*
2+
Package formatting -- 主に fmt.Printf() で指定できる verb や adverb についてのサンプルが配置されています.
3+
*/
4+
package formatting
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package formatting
2+
3+
import (
4+
"github.com/devlights/try-golang/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["formatting_adverb_explicit_argument_indexes"] = AdverbExplicitArgumentIndexes
19+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package formatting
2+
3+
import "fmt"
4+
5+
// AdverbExplicitArgumentIndexes -- フォーマッティングの Explicit argument indexes についてのサンプルです。
6+
//
7+
// REFERENCES:
8+
// - https://golang.org/pkg/fmt/
9+
func AdverbExplicitArgumentIndexes() error {
10+
// ------------------------------------------------------------
11+
// Go のフォーマット仕様には
12+
// Explicit argument indexes
13+
// という仕様がある。
14+
//
15+
// フォーマット文字列内に以下が指定できる
16+
// - [1]とすると、添字内のパラメータの値を指定できる
17+
// 中の数字は、フォーマット文字列の後のパラメータの位置
18+
// (最初のパラメータが 1 )
19+
// - [1]* のようにアスタリスクをつけると、そのパラメータの
20+
// 値を指示子の値として利用できる
21+
// ------------------------------------------------------------
22+
23+
// 以下の2つは同じ結果となる
24+
i := 10
25+
fmt.Printf("%d(%T)\n", i, i)
26+
fmt.Printf("%d(%[1]T)\n", i)
27+
28+
// 以下の2つは同じ結果となる
29+
f := 32.8
30+
fmt.Printf("%3.2f\n", f)
31+
fmt.Printf("%[3]*.[2]*[1]f\n", f, 2, 3)
32+
33+
// 複雑になるので、あまり意味はないが、以下の様にも出来る
34+
var (
35+
numberLength = 10
36+
totalLength = numberLength + 5
37+
)
38+
39+
fmt.Printf("% [2]*[1]s\n", fmt.Sprintf("%0[2]*[1]d\n", i, numberLength), totalLength)
40+
41+
return nil
42+
}

0 commit comments

Comments
 (0)