title: What is the correct way to find the min between two integers in Go?
body: <p>I imported the math library in my program, and I was trying to find the minimum of three numbers in the following way:</p>
  
  <p><code>v1[j+1] = math.Min(v1[j]+1, math.Min(v0[j+1]+1, v0[j]+cost))</code></p>
  
  <p>where v1 is declared as:</p>
  
  <pre><code>t := "stackoverflow"
  v1 := make([]int, len(t)+1)
  </code></pre>
  
  <p>However, when I run my program I get the following error:</p>
  
  <pre><code>./levenshtein_distance.go:36: cannot use int(v0[j + 1] + 1) (type int) as type float64 in argument to math.Min
  </code></pre>
  
  <p>I thought it was weird because I have another program where I write</p>
  
  <p><code>fmt.Println(math.Min(2,3))</code></p>
  
  <p>and that program outputs <code>2</code> without complaining.</p>
  
  <p>so I ended up casting the values as float64, so that <code>math.Min</code> could work:</p>
  
  <pre><code>v1[j+1] = math.Min(float64(v1[j]+1), math.Min(float64(v0[j+1]+1), float64(v0[j]+cost)))
  </code></pre>
  
  <p>With this approach, I got the following error:</p>
  
  <pre><code>./levenshtein_distance.go:36: cannot use math.Min(int(v1[j] + 1), math.Min(int(v0[j + 1] + 1), int(v0[j] + cost))) (type float64) as type int in assignment
  </code></pre>
  
  <p>so to get rid of the problem, I just casted the result back to <code>int</code></p>
  
  <p>I thought this was extremely inefficient and hard to read:</p>
  
  <pre><code>v1[j+1] = int(math.Min(float64(v1[j]+1), math.Min(float64(v0[j+1]+1), float64(v0[j]+cost))))
  </code></pre>
  
  <p>I also wrote a small <code>minInt</code> function, but I think this should be unnecessary because the other programs that make use of <code>math.Min</code> work just fine when taking integers, so I concluded this has to be a problem of my program and not the library per se.</p>
  
  <p>Is there anything that I'm doing terrible wrong? </p>
  
  <p>Here's a program that you can use to reproduce the issues above, line 36 specifically:
  package main</p>
  
  <pre><code>import (
      "math"
  )
  
  func main() {
      LevenshteinDistance("stackoverflow", "stackexchange")
  }
  
  func LevenshteinDistance(s string, t string) int {
      if s == t {
          return 0
      }
      if len(s) == 0 {
          return len(t)
      }
      if len(t) == 0 {
          return len(s)
      }
  
      v0 := make([]int, len(t)+1)
      v1 := make([]int, len(t)+1)
  
      for i := 0; i &lt; len(v0); i++ {
          v0[i] = i
      }
  
      for i := 0; i &lt; len(s); i++ {
          v1[0] = i + 1
          for j := 0; j &lt; len(t); j++ {
              cost := 0
              if s[i] != t[j] {
                  cost = 1
              }
              v1[j+1] = int(math.Min(float64(v1[j]+1), math.Min(float64(v0[j+1]+1), float64(v0[j]+cost))))
          }
  
          for j := 0; j &lt; len(v0); j++ {
              v0[j] = v1[j]
          }
      }
      return v1[len(t)]
  }
  </code></pre>