KazuminEngine

プログラマーの日記

A simple tip : Big O notation via found element in golang.

I found great thing. It's a fast Big O notation go programing.If you don't know Big O notation, read next link .

Big O notation - Wikipedia, the free encyclopedia

If you always scratch programing in Ruby,but now in Go.If you wanna call look like a method which look like include? in Ruby in Golang.In the first place , do you know a include? method. which found argment element from receive object of list.There is similar Two way.which call look like it in go

First.

for loop.which is O(n).scratch below.

package main

import (
    "fmt"
)

func main(){
    var target [5]string = [5]string{"0","1","2","3","4"}

    for value := range target{
        if 3 == value {
            fmt.Println("match")
        }
    }
}

Second.

golang don't have to use list in this patarn. It should use map,which is O(1).like scratch below.

package main

import (
    "fmt"
)

func main(){
    map1 := map[string]int{"0":0,"1":1,"2":2,"3":3,"4":4}
    _, ok := map1["0"]
    fmt.Println(ok)
}

map is faster than loop implemention.and below golang code is very cool. In the first place, golang thought is very cool.

 _, ok := map1["0"]