give me fibonacci code in golang
- User
give me fibonacci code in golang
Assistant
Here is a function to generate the Fibonacci sequence in Go:
gofunc fibonacci(n int) int { if n <= 1 { return n } return fibonacci(n-1) + fibonacci(n-2) }To generate the first 10 Fibonacci numbers:
gofunc main() { for i := 0; i < 10; i++ { fmt.Println(fibonacci(i)) } }This implements the Fibonacci sequence recursively. An iterative implementation would be:
gofunc fibonacci(n int) int { a, b := 0, 1 for i := 0; i < n; i++ { a, b = b, a+b } return a }The iterative approach is generally preferred over recursive for Fibonacci as it is more efficient for larger inputs.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.