株式会社ヴァンデミックシステム

Blog

<スポンサーリンク>

wslにセットアップ

インストール

sudo apt-get -y install golang-go

環境変数

vim ~/.bashrc
# goenv
export GOENV_ROOT=$HOME/.goenv
export PATH=$GOENV_ROOT/bin:$PATH
eval "$(goenv init -)"

goenvインストール

git clone https://github.com/syndbg/goenv.git ~/.goenv
exec $SHELL
source ~/.bashrc
goenv install -l
goenv install 1.14.4
goenv global 1.14.4

バージョン確認

yuta@DESKTOP-V36210S:/mnt/c/Users/yuta$ goenv versions
  system
* 1.14.4 (set by /home/yuta/.goenv/version)

文字列置換

package main

import (
    "fmt"
    "strings"
)

func main() {
    fmt.Println("Hello world")
    fmt.Println("Hello" + "  world")
    fmt.Println(string("Hello world"[0]))

    fmt.Println("---置き換え---")
    var s string = "Hello world"
    fmt.Println(strings.Replace(s, "H", "X", 1))
    fmt.Println(s)
    fmt.Println(strings.Contains(s, "world"))
}

出力

Hello world
Hello  world
H
---置き換え---
Xello world
Hello world
true

論理値

package main

import "fmt"

func main() {
    t, f := true, false
    fmt.Printf("%T %v %t\n", t, t, f)
    fmt.Printf("%T %v %t\n", f, f, t)
}

出力結果

bool true false
bool false true
Process exiting with code: 0
  • %tで型を強制する
  • 数値を入れると怒られる
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, 0)
}

出力

bool true %!t(int=1)
bool false %!t(int=0)

論理演算子

package main

import "fmt"

func main() {
    // and
    fmt.Println(true && true)
    fmt.Println(true && false)
    fmt.Println(false && false)

    // or
    fmt.Println(true || true)
    fmt.Println(true || false)
    fmt.Println(false || false)

    // not
    fmt.Println(!true)
    fmt.Println(!false)
}

出力

true
false
false
true
true
false
false
true

型変換

package main

import (
    "fmt"
    "strconv"
)

func main() {
    // int → float
    var x int = 1
    xx := float64(x)
    fmt.Printf("%T %v %f\n", xx, xx, xx)

    // float64 → int
    var y float64 = 1.2
    yy := int(y)
    fmt.Printf("%T %v %d\n", yy, yy, yy)

    // string コンバートする必要があるASCII to Integer
    var s string = "14"
    // 「_」の部分にエラーが入る errとかにしてif err みたいな感じでエラーハンドリングする
    i, _ := strconv.Atoi(s)
    fmt.Printf("%T %v", i, i)
}

出力結果

float64 1 1.000000
int 1 1
int 14
Process exi

配列

package main

import "fmt"

func main() {
    var a [2]int
    a[0] = 100
    a[1] = 200
    fmt.Println(a)

    var b [2]int = [2]int{100, 200}
    fmt.Println(b)
}
[100 200]
[100 200]
package main

import "fmt"

func main() {
    var a [2]int
    a[0] = 100
    a[1] = 200
    fmt.Println(a)

    /*
        var b [2]int = [2]int{100, 200}
        fmt.Println(b)
    */

    // 要素数を増やす場合、配列ではなくスライス
    var b []int = []int{100, 200}
    b = append(b, 300)
    fmt.Println(b)
}
[100 200]
[100 200 300]
Process exiti
package main

import "fmt"

func main() {
    n := []int{1, 2, 3, 4, 5, 6}
    fmt.Println(n)

    // 2番目
    fmt.Println(n[2])

    // 2-4
    fmt.Println(n[2:4])

    // 0-2
    fmt.Println(n[:2])

    // 2-最後の要素
    fmt.Println(n[2:])

    // 全部
    fmt.Println(n[:])

    // 2番目のみ書き換え
    n[2] = 100
    fmt.Println(n)

    // 入れ子
    var board = [][]int{
        []int{0, 1, 2},
        []int{3, 4, 5},
        []int{6, 7, 8},
    }
    fmt.Println(board)

    // 追加
    n = append(n, 100, 200, 300, 400)
    fmt.Println(n)
}

出力

[1 2 3 4 5 6]
3
[3 4]
[1 2]
[3 4 5 6]
[1 2 3 4 5 6]
[1 2 100 4 5 6]
[[0 1 2] [3 4 5] [6 7 8]]
[1 2 100 4 5 6 100 200 300 400]

make 配列作る

package main

import "fmt"

func main() {
    // 長さが3, 容量が5(メモリが確保する部分)
    n := make([]int, 3, 5)
    fmt.Printf("len=%d cap=%d value=%v\n", len(n), cap(n), n)

    // 要素を2つ追加
    n = append(n, 0, 0)
    fmt.Printf("len=%d cap=%d value=%v\n", len(n), cap(n), n)

    // 要素を2つ追加
    n = append(n, 1, 2, 3, 4, 5)
    fmt.Printf("len=%d cap=%d value=%v\n", len(n), cap(n), n)

    // 次の書き方だと長さ、容量両方3になる
    a := make([]int, 3)
    fmt.Printf("len=%d cap=%d value=%v\n", len(a), cap(a), a)

    // bだとメモリ確保する、cだとメモリ確保しない書き方
    b := make([]int, 0)
    var c []int
    fmt.Printf("len=%d cap=%d value=%v\n", len(b), cap(b), b)
    fmt.Printf("len=%d cap=%d value=%v\n", len(c), cap(c), c)
}

出力

len=3 cap=5 value=[0 0 0]
len=5 cap=5 value=[0 0 0 0 0]
len=10 cap=10 value=[0 0 0 0 0 1 2 3 4 5]
len=3 cap=3 value=[0 0 0]
len=0 cap=0 value=[]
len=0 cap=0 value=[]

<スポンサーリンク>

コメントを残す

Allowed tags:  you may use these HTML tags and attributes: <a href="">, <strong>, <em>, <h1>, <h2>, <h3>
Please note:  all comments go through moderation.

*

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)