0
0

More than 3 years have passed since last update.

【Golang】型と型変換

Posted at

【Golang】型と型変換

Golangの基礎学習〜Webアプリケーション作成までの学習を終えたので、復習を兼ねてまとめていく。
基礎〜応用まで。

package main


import (
    "fmt"
    "strings"
    "strconv"
)




func main() {
    //数値型
    //いろんな種類がある
    //https://golang.org/ref/spec

    //int は自分のPCの32ビットか64ビットの環境依存になる
    //確実に宣言したい場合はint64などにする
    //宣言するビット数で最大値の範囲が異なる。
    var (
        //uint 整数+
        u8  uint8     = 255
        //int 整数 - ~ +
        i8  int8      = 127
        //浮動小数点数
        f32 float32   = 0.2
        //複素数
        c64 complex64 = -5 + 12i

        //上記のように、揃えて書くのがGo推奨
    )
    //表示
    fmt.Println(u8, i8, f32, c64)
    //>>255 127 0.2 (-5+12i)

    //fmt.Printf()で型を調べる
    //%T Type
    //%v Value そのままの値が入る
    //%t boolean
    //その他ドキュメント確認
    fmt.Printf("type=%T value=%v", u8, u8)
    //>>type=uint8 value=2552



    //演算 pythonと同じ
    //変数宣言時にはスペースをあける 1 + 1
    //println 内での式はあけない
    x := 1 + 1
    fmt.Println(x)
    fmt.Println(1+1, 2+2)
    fmt.Println("1 + 1 =", 1+1)
    fmt.Println("10 - 1 =", 10-1)
    fmt.Println("10 / 2 =", 10/2)
    fmt.Println("10 / 3 =", 10/3)
    fmt.Println("10.0 / 3 =", 10.0/3)
    fmt.Println("10 / 3.0 =", 10/3.0)
    fmt.Println("10 % 2 =", 10%2)
    fmt.Println("10 % 3 =", 10%3)
    /*
    2 4
    1 + 1 = 2
    10 - 1 = 9
    10 / 2 = 5
    10 / 3 = 3
    10.0 / 3 = 3.3333333333333335
    10 / 3.0 = 3.3333333333333335
    10 % 2 = 0
    10 % 3 = 1
    */




    //go fmt
    //書式を修正する場合は go fmt --.go
    //実際に修正して書き換える場合は go fmt -w --.go




    //++ --
    x = 0
    fmt.Println(x)
    //>>0

    //1足す
    x++
    fmt.Println(x)
    //>>1

    //-1する
    x--
    fmt.Println(x)
    //>>0





    //shift
    //2進数をずらす。あまり使わない?
    fmt.Println(1 << 0)
    //1
    //1を1回シフト == 2
    fmt.Println(1 << 1)
    //4
    fmt.Println(1 << 2)
    //8
    fmt.Println(1 << 3)


    //###############################################################



    //文字列型
    fmt.Println("Hello World")
    //>>Hello World

    //分割
    fmt.Println("Hello " + "World")
    //>>Hello World



    //1文字目表示 
    //"Hello World"[0]だけでは、アスキーコード
    //なのでstring()で囲む
    fmt.Println(string("Hello World"[0]))
    //>>H


    //文字の置き換え
    var s string = "Hello World"
    //s[0] = "X"はできない
    //Replace 置き換え (どの文字列の,どの文字を、どの文字にする、何個)
    s = strings.Replace(s, "H", "X", 1)

    //コピーを作成するので、元データは変わらないが、
    //上記のように、変数を上書きすることで元データを書き換えられる
    fmt.Println(s)
    //>>Xello World



    //strings.Contains() 第一引数に、第二引数の文字列が含まれているかを判定
    fmt.Println(strings.Contains(s, "World"))
    //>>true



    //バッククォートで囲めばそのまま表示される。
    fmt.Println(`Test
                       Test
Test`)
    /*
    Test
                       Test
Test
    */


    //""を表示したい場合 \"  もしくは``で囲む
    fmt.Println("\"")
    fmt.Println(`"`)
    //>>"
    //>>"





    //###############################################################

    //論理値型
    //宣言
    // var t, f bool = true, false
    t, f := true, false

    //%t でbool型でないと正しく表示しない。厳格にしたい場合使う
    fmt.Printf("%T %v %t\n", t, 1, t)
    fmt.Printf("%T %v %t\n", f, 0, f)
    //>>bool 1 true
    //>>bool 0 false

    //論理演算子
    //&& かつ
    //True
    fmt.Println(true && true)
    //false
    fmt.Println(true && false)
    fmt.Println(false && false)

    //|| もしくは
    //true
    fmt.Println(true || true)
    //true
    fmt.Println(true || false)
    fmt.Println(false || false)

    //! 否定 でなければ
    //false
    fmt.Println(!true)
    //true
    fmt.Println(!false)


    //###############################################################



    //型変換
    //数値型は簡単
    var x2 int = 1
    //float64() 新しいxxに代入
    xx := float64(x2)
    //%f float表記
    fmt.Printf("%T %v %f\n", xx, xx, xx)
    //>>float64 1 1.000000

    var y float64 = 1.2
    //新しいyyに代入
    yy := int(y)
    //%d int表記
    fmt.Printf("%T %v %d\n", yy, yy, yy)
    //>>int 1 1




    //文字列→数値変換
    //文字列型は難しい
    //そのまま変換することはできない
    //Pythonとかならそのままできるが、Goはできない
    var s2 string = "14"

    //Atoi アスキートゥインテジャー
    //strconv.Atoi() アスキー文字をintegerに変換
    //i, _ は返り値が値とerrorと2つある為、片方は_にしている。
    //もしerrならというエラーハンドリングできる。
    //_ にすることで、使われていない変数でのエラーを回避する。
    //こういうエラーはビルドした時に発生するので常に注意する。
    i, _ := strconv.Atoi(s2)
    fmt.Printf("%T %v\n", i, i)
    //>>int 14




    //文字列をバイト配列から文字列に戻して1文字目だけ表示
    h := "Hello World"
    fmt.Println(string(h[0]))
    //>>H




    //数値→文字列変換
    var i3 int=321
    var s3 string
    s3 = strconv.Itoa(i3)
    fmt.Println(s3) 
    //>>321

}

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0