Skip to content

Instantly share code, notes, and snippets.

@mathisve
Last active September 26, 2020 10:19
Show Gist options
  • Save mathisve/7998c03d29bf1a616bb6d22f8922e920 to your computer and use it in GitHub Desktop.
Save mathisve/7998c03d29bf1a616bb6d22f8922e920 to your computer and use it in GitHub Desktop.
calculate displacement at time t using functions as variables in golang
package main
import (
"fmt"
"math"
)
func main() {
var acceleration, velocity, displacement, time float64
fmt.Print("Enter the acceleration: ")
fmt.Scan(&acceleration)
fmt.Print("Enter the velocity: ")
fmt.Scan(&velocity)
fmt.Print("Enter the displacement: ")
fmt.Scan(&displacement)
fmt.Print("Enter the time: ")
fmt.Scan(&timeS)
fn := GenDisplaceFn(acceleration, velocity, displacement)
fmt.Printf("The displacement at time t=%s is: %v\n", time, fn(time))
}
func GenDisplaceFn(acceleration, velocity, displacement float64) func (time float64) (float64) {
return func (time float64) (float64){
return float64(1/2) * acceleration * math.Exp(time) + velocity * time + displacement
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment