SoFunction
Updated on 2025-03-05

Go implements a simple unit conversion method of feet and meters

Look at the code ~

package main 
import (
    "fmt"
    "os"    
    "strconv"
)
 
type Feet float64
type Meter float64
 
func main() {
    var transType string = [2]
    var transferObject string = [1]
    parseObj, _ := (transferObject, 64)
 
    if transType == "feet" {
        // transfer to meter
        (toFeet(Meter(parseObj)))
    } else {
        (toMeter(Feet(parseObj)))
    } 
} 
 
func toMeter(feet Feet) Meter {
    return Meter(feet * 0.3048)
}
 
func toFeet(meter Meter) Feet {
    return Feet(meter * 1 / 0.3048)
}  
// After saving go run  1 feet Output1rice=How many feet

Supplement: Golang Go language Convert file size byte units EB TB GB MB KB B Reserved decimals

Look at the code ~

// Unit conversion of bytes retains two decimal placesfunc formatFileSize(fileSize int64) (size string) {
   if fileSize < 1024 {
      //return (fileSize, 10) + "B"
      return ("%.2fB", float64(fileSize)/float64(1))
   } else if fileSize < (1024 * 1024) {
      return ("%.2fKB", float64(fileSize)/float64(1024))
   } else if fileSize < (1024 * 1024 * 1024) {
      return ("%.2fMB", float64(fileSize)/float64(1024*1024))
   } else if fileSize < (1024 * 1024 * 1024 * 1024) {
      return ("%.2fGB", float64(fileSize)/float64(1024*1024*1024))
   } else if fileSize < (1024 * 1024 * 1024 * 1024 * 1024) {
      return ("%.2fTB", float64(fileSize)/float64(1024*1024*1024*1024))
   } else { //if fileSize < (1024 * 1024 * 1024 * 1024 * 1024 * 1024)
      return ("%.2fEB", float64(fileSize)/float64(1024*1024*1024*1024*1024))
   }
}

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.