How to run existing program with yaegi cli and extend program’s functionality #1505
-
It is said that yaegi makes Go programs extensible, how can I test this on a program, in an interactive shell using yaegi cli, I made a simple program that gives the average which resides in the main(), and I want to extend the program by adding a new function that gives the median, now how can do this on an existing program using yaegi-cli, I'm just trying to learn what yaegi is, here the code of the program package main
import "fmt"
func main() {
nums := []int{1, 2, 3, 4, 5}
sum := 0
for _, num := range nums {
sum += num
}
average := float64(sum) / float64(len(nums))
fmt.Println("The average is", average)
} and in the terminal, I enter yaegi interactive shell, now I want to test/make changes with respect to this program, like adding a new function that calculates the median, here's the program func median(nums []int) float64 {
sorted := make([]int, len(nums))
copy(sorted, nums)
sort.Ints(sorted)
length := len(sorted)
if length % 2 == 0 {
return float64(sorted[length/2]+sorted[(length/2)-1]) / 2.0
}
return float64(sorted[length/2])
} and run the $ yaegi run -i main.go
The average is 3
> func median(nums []int) float64 {
sorted := make([]int, len(nums))
copy(sorted, nums)
sort.Ints(sorted)
length := len(sorted)
if length % 2 == 0 {
return float64(sorted[length/2]+sorted[(length/2)-1]) / 2.0
}
return float64(sorted[length/2])
}
The average is 3
: 0xc000284330
> fmt.Println("The median is", median(nums))
main.go:1:64: undefined: nums |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 12 replies
-
Hi. what you're trying to do cannot work, because the nums variable from the main is local to the main function, it is not a global scope variable. So it is unknown when you're trying to give it as an argument to the median func(). It's as if you were trying to run that code: package main
import (
"fmt"
"sort"
)
func oldmain() {
nums := []int{1, 2, 3, 4, 5}
sum := 0
for _, num := range nums {
sum += num
}
average := float64(sum) / float64(len(nums))
fmt.Println("The average is", average)
}
func median(nums []int) float64 {
sorted := make([]int, len(nums))
copy(sorted, nums)
sort.Ints(sorted)
length := len(sorted)
if length%2 == 0 {
return float64(sorted[length/2]+sorted[(length/2)-1]) / 2.0
}
return float64(sorted[length/2])
}
func main() {
oldmain()
fmt.Println("The median is", median(nums))
} Here is how it would look if you fixed your main to have a globar var instead: % cat ./main.go
package main
import (
"fmt"
"sort"
)
var nums = []int{1, 2, 3, 4, 5}
func main() {
sum := 0
for _, num := range nums {
sum += num
}
average := float64(sum) / float64(len(nums))
fmt.Println("The average is", average)
}
%
%
% yaegi run -i ./main.go
The average is 3
> func median(nums []int) float64 {
sorted := make([]int, len(nums))
copy(sorted, nums)
sort.Ints(sorted)
length := len(sorted)
if length%2 == 0 {
return float64(sorted[length/2]+sorted[(length/2)-1]) / 2.0
}
return float64(sorted[length/2])
}
The average is 3
: 0xc00020e2b0
>
> fmt.Println("The median is", median(nums))
The median is 3 |
Beta Was this translation helpful? Give feedback.
Hi.
what you're trying to do cannot work, because the nums variable from the main is local to the main function, it is not a global scope variable. So it is unknown when you're trying to give it as an argument to the median func(). It's as if you were trying to run that code: