[box class="blue_box" title="☑本記事の内容"]
- 数値型
- 文字列型
- 論理値型
[/box]
参考資料:現役シリコンバレーエンジニアが教えるGo入門 + 応用でビットコインのシストレFintechアプリの開発
[box class="glay_box" title="Contents"]
[/box]
数値型
[box class="yellow_box" title="✔数値型の目次"]
[/box]
数値型に関して
基礎的な型
[box class="glay_box"]
- https://golang.org/ref/spec#Numeric_typesに数値型が記載
- Printfは%Tで型の表示、%vで値の出力
[/box]
package main
import "fmt"
func main() {
var (
u8 uint8 = 255
i8 int8 = 127
f32 float32 = 0.2
c64 complex64 = -5 + 12i
)
fmt.Println(u8, i8, f32, c64)
fmt.Printf("type=%T value=%v", u8, u8)
}
(結果)
255 127 0.2 (-5+12i)
type=uint8 value=255
四則演算
[box class="glay_box"]
- 整数のみを使用した際には結果も整数となる
- 片方だけでも少数を使用すると結果は少数まで表示される
- %で余りの計算が出来る
[/box]
package main
import "fmt"
func main() {
fmt.Println("1 + 1 =", 1+1)
fmt.Println("10 - 1 =", 10-1)
fmt.Println("10 / 3 =", 10/3)
fmt.Println("10.0 / 3 =", 10.0/3)
fmt.Println("10 % 2 =", 10%2)
fmt.Println("10 % 3 =", 10%3)
}
(結果)
1 + 1 = 2
10 - 1 = 9
10 / 3 = 3
10.0 / 3 = 3.3333333333333335
10 / 3.0 = 3.3333333333333335
10 % 2 = 0
10 % 3 = 1
値の増減とシフト
[box class="glay_box"]
- 増減は++,--で出来る
- (1 << 0)とすると1がシフトして値が変更される※2進数の増減
[/box]
package main
import "fmt"
func main() {
x := 0
fmt.Println(x)
x++
fmt.Println(x)
x--
fmt.Println(x)
fmt.Println(1 << 0)
fmt.Println(1 << 1)
fmt.Println(1 << 2)
fmt.Println(1 << 3)
}
(結果)
0
1
0
1
2
4
8
文字列型
[box class="yellow_box" title="✔文字列型の目次"]
[/box]
文字列型に関して
文字の繋ぎ合わせ
[box class="glay_box"]
- +をつけることで文字をつなげ合わせることが出来る
[/box]
package main
import "fmt"
func main() {
fmt.Println("Hello " + "World")
}
(結果)
Hello World
文字の抽出
[box class="glay_box"]
- 文字列の中で指定した文字を抽出したいときはstring()を使用する
[/box]
package main
import "fmt"
func main() {
fmt.Println(string("Hello World"[0]))
}
(結果)
H
文字の置き換えと存在確認
[box class="glay_box"]
- stringsを使用することで文字の置き換えを行える
- stringとは別なので要注意
- Replaceで文字の置き換え、Containsで文字の存在確認が行われる
- 文字が存在していればtrue、していなければfalse
[/box]
package main
import (
"fmt"
"strings"
)
func main() {
var s string = "Hello World"
s = strings.Replace(s, "H", "X", 1)
fmt.Println(s)
fmt.Println(strings.Contains(s, "World"))
}
(結果)
Xello World
true
特殊な表示
[box class="glay_box"]
- バッククォート(`)を使用することによって改行を反映させることが出来る
- \を使用することでダブルクォート(")を文字として使用することが出来る
- バッククォートでダブルクォートを囲う事でも使用可能。
[/box]
package main
import (
"fmt"
)
func main() {
fmt.Println(`Test
Test
Test`)
fmt.Println("\"")
fmt.Println(`"`)
}
Test
Test
Test
"
論理値型
[box class="yellow_box" title="✔論理値型の目次"]
[/box]
基本的なこと
[box class="glay_box"]
- true, falseに関して
- fmt.printfの%tを使用することで論理値型でないと出力されなくする
[/box]
package main
import "fmt"
func main() {
t, f := true, false
fmt.Printf("%T %v %t\n", t, t, 1)
fmt.Printf("%T %v %t\n", f, f, 5)
}
(結果)
bool true %!t(int=1)
bool false %!t(int=5)
and,or,notの使い方
[box class="glay_box"]
- && でand, || でor, !でnotを表す
[/box]
package main
import "fmt"
func main() {
fmt.Println(true && true)
fmt.Println(true && false)
fmt.Println(false && false)
fmt.Println("===========")
fmt.Println(true || true)
fmt.Println(true || false)
fmt.Println(false || false)
fmt.Println("===========")
fmt.Println(!true)
fmt.Println(!false)
}
(結果)
true
false
false
true
true
false
false
true