Giter Club home page Giter Club logo

Comments (4)

diegoparra avatar diegoparra commented on August 22, 2024

Minha solução é basicamente igual porém pra primeira função eu crio a variável já chamando a função e depois faço o print do valor:

https://play.golang.org/p/XrA1NCmZ7QJ

from aprendago.

an4kein avatar an4kein commented on August 22, 2024

https://play.golang.org/p/ZuSobQ3z_g7

No início fiquei com dúvidas em relação como criar uma função que receba um valor do (type of slice).. Entre colocar (x ..int) and (x []int), mas após fazer do meu jeito consegui chegar no resultado. Adicionei outra função (teste) logo apos assistir o video e sanar minha dúvida.

package main

import "fmt"

/* - Crie uma função que receba um parâmetro variádico do tipo int e retorne a soma de todos os ints recebidos;
- Passe um valor do tipo slice of int como argumento para a função;
- Crie outra função, esta deve receber um valor do tipo slice of int e retornar a soma de todos os elementos da slice;
- Passe um valor do tipo slice of int como argumento para a função.
*/

func vari(x ...int) int {
	soma := 0
	for _, i := range x {
		soma += i
	}
	return soma
}

func recebevalor(x ...int) int {
	soma := 0
	for _, i := range x {
		soma += i
	}
	return soma
}

func teste(x []int) int {
	soma := 0
	for _, v := range x {
		soma += v
	}
	return soma
}

func main() {
	fmt.Println(vari(1, 2))
	fmt.Println(recebevalor([]int{2, 2}...))
	fmt.Println(teste([]int{5, 5}))
}

Output

3
4
10

Program exited.

from aprendago.

alansantosmg avatar alansantosmg commented on August 22, 2024

My solution:

/* - Crie uma função que receba um parâmetro variádico do tipo int e
retorne a soma de todos os ints recebidos;
- Passe um valor do tipo slice of int como argumento para a função;
- Crie outra função, esta deve receber um valor do tipo slice of int e
retornar a soma de todos os elementos da slice;
- Passe um valor do tipo slice of int como argumento para a função.
*/

package main

import "fmt"

func main() {
	
	s := []int{1, 2, 3, 4, 5}

	fmt.Println(variação(s...))
	fmt.Println(variação2(s))
}

func variação(x ...int) (total int) {
	for v := range x {
		total += x[v]
	}
	return
}
func variação2(y []int) (total int) {
	for v := range y {
		total += y[v]
	}
	return
}

from aprendago.

wfrsilva avatar wfrsilva commented on August 22, 2024

Cap. 13 – Exercícios: Nível #6 – 2
https://go.dev/play/p/FtEIO5xzSRk

image

from aprendago.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.