Monday, April 01, 2019

Simple GO Codes Functions

Golang  function to fill a specific default value into an Array
Simple code to achieve that

func filldefault(YourArray []int ) {
   for i := range YourArray { YourArray[i] = -1 }
}

Golang function of max and min
Simple code to achieve that

func max(x, y int) {
   if x > y {
        return x
   } else {
       return y
        }
}

func min(x, y int) {
   if x > y {
        return y
   } else {
       return x
       }
}

Golang  function of contain or find an element in an array
Simple code to achieve that

func contain(t []int, x int) bool {
       for _, n := range t {
        if n == x {
          return true
       }
     }
   return false

}

No comments:

Post a Comment