go言語の初歩的な理解

16766 ワード

// myNote1 project main.go
package main

//go         ,   java     ,     
import (
    "fmt"
)

var ( //                       
    aa int
    bb bool
)

func main() {
    //    
    fmt.Println("Hello World!")

    //    
    var i int
    i = 10
    var j = "jjjjjjjjjjjj"
    k := "kkkkkkkkkkk"
    fmt.Println("  :", i)
    fmt.Println("  :", j)
    fmt.Println("  :", k)
    //      
    var s, u, g int
    s, u, g = 10, 20, 30
    fmt.Println("  :", s)
    fmt.Println("  :", u)
    fmt.Println("  :", g)

    var a, b, c = "aaa", "bbb", "ccc"
    fmt.Println("  :", a)
    fmt.Println("  :", b)
    fmt.Println("  :", c)

    d, e, f := "ddd", "eee", "fff"
    fmt.Println("  :", d)
    fmt.Println("  :", e)
    fmt.Println("  :", f)

    //    
    fmt.Println("    :", aa)
    fmt.Println("    :", bb)

    //         
    fmt.Println("    :", &a)

    //        ,        ,    jj declared and not used
    //var jj string = "jk"

    //                      
    //const          
    const LEFT int = 100
    const RIGHT = 200
    const UP, DOWN = 300, 400
    const (
        BEFORE = 500
        AFTER  = 600
    )
    fmt.Println("  :", LEFT, RIGHT, UP, DOWN, BEFORE, AFTER)

    //  
    var v, z int = 10, 20

    fmt.Println("  :", v+z)
    fmt.Println("  :", v-z)
    fmt.Println("  :", v*z)
    fmt.Println("  :", v/z)
    fmt.Println("  :", v%z)
    fmt.Println("    :", v <= z)
    fmt.Println("  :", v == z)



    //if else elseif
    if 10 == 10 {
        fmt.Println("if  ")
    }

    if 10 == 20 {
        fmt.Println("if  ")
    } else {
        fmt.Println("else  ")
    }
    //switch
    jk := "a"
    switch jk {
    case "a":
        fmt.Println("    ")
        break
    case "b":
        fmt.Println("xxxxx")
        break
    default:
        fmt.Println("--------")
        break
    }


    var index = 1

    for index < 4 {
        fmt.Println("-------")
        index++
    }


    var flat int = 3
A:
    index = 1
    for index < 3 {
        fmt.Println("xxxx")
        index++
    }
    flat++
    if flat < 5 {
        goto A
    }


    testFun1()
    testFun2(10, 20)
    fmt.Println("    :", testFun3(10, 20))
    x, y := testFun4(10, 20)
    fmt.Println("       :", x, y)


    var arr [3]int
    arr[0] = 1
    arr[1] = 10
    arr[2] = 32
    {
        var op int = 0
        for op < len(arr) {
            fmt.Println(arr[op])
            op++
        }

    }
    //        
    var arr1 = [...]float64{12.12, 34.2, 45.2}
    fmt.Println(arr1)
    var arr2 = [5]float32{11.23, 343.23, 34.5}
    fmt.Println(arr2)

    //      
    var in int = 100
    fmt.Println("     ", &in)
    //            
    var ini *int
    ini = &in
    fmt.Println("      ", *ini)
    //   
    var ink *int
    fmt.Println("     ", (ink == nil))


    //             ,                       。     java     ,           
    type Book struct {
        title string
        num   int
        names []float32
        flag  bool
    }
    var book1 Book
    book1.flag = true
    book1.num = 10
    book1.title = "   "
    book1.names = []float32{1000.0, 2.0, 3.4, 7.0, 50.0}
    fmt.Println(book1)


    //Go          ,                ,Go        ,
    //           ("    "),               ,      ,              。
    //     java    

    //    make  :T              。
    //   make       、    (           )          。   make  ,         ,           。
    //var numbers []int = make([]int, 5, 5)

    numbers := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}

    fmt.Println(numbers)
    fmt.Println("numbers[1:4] ==", numbers[1:4])
    /*       0*/
    fmt.Println("numbers[:3] ==", numbers[:3])
    /*       len(s)*/
    fmt.Println("numbers[4:] ==", numbers[4:])
    fmt.Println("       :", cap(numbers))
    fmt.Println("       :", len(numbers))

    numbers = append(numbers, 13)
    fmt.Println("     :", numbers)

    //     9   
    numbers = append(numbers[:9], numbers[10:]...)
    fmt.Println("          :", numbers)

    //           
    numbers = append(numbers[:len(numbers)-1], numbers[len(numbers):]...)
    fmt.Println("           :", numbers)

    fmt.Println("       :", len(numbers))

    var number1 []int
    if number1 == nil {
        fmt.Println("     ")
    }


    //  map
    var countryCapitalMap map[string]string
    /*      */
    countryCapitalMap = make(map[string]string)

    /* map    key-value  ,          */
    countryCapitalMap["France"] = "Paris"
    countryCapitalMap["Italy"] = "Rome"
    countryCapitalMap["Japan"] = "Tokyo"
    countryCapitalMap["India"] = "New Delhi"

    fmt.Println(countryCapitalMap["India"])

    /*    key    map   */
    for country := range countryCapitalMap {
        fmt.Println("Capital of", country, "is", countryCapitalMap[country])
    }

    /*              */
    captial, ok := countryCapitalMap["India"]
    /*    ok   true,    ,      */
    if ok {
        fmt.Println("Capital of India is----", captial)
    } else {
        fmt.Println("Capital of United States is not present")
    }


    //       
    var phone Phone
    phone = new(IPhone)
    phone.call()

    phone = new(XiaoMi)
    phone.call()
}

//  
func testFun1() {
    fmt.Println("  ")
}

func testFun2(num1 int, num2 int) {
    fmt.Println("    ", num1+num2)
}

//       
func testFun3(num1 int, num2 int) int {
    return num1 + num2
}

//       
func testFun4(num1 int, num2 int) (int, int) {
    return num1 + num2, num1 - num2
}

//                          
type Phone interface {
    //      
    call()
}

//     
type IPhone struct{}

//        
func (iphone IPhone) call() {
    fmt.Println("        ")
}

//        
type XiaoMi struct{}

//        
func (xiaoMi XiaoMi) call() {
    fmt.Println("        : ")
}