Last active
October 3, 2022 18:53
-
-
Save devrsantos/e431d5cea4136294e2b918283c73d1e4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
/***********************************************/ | |
func Funcao_Modelo(n1, n2 int) int { | |
return n1 + n2 | |
} | |
/***********************************************/ | |
func UsandoIf_Else() { | |
if i := true; i != false { | |
fmt.Println("Esse If foi inicializado com uma variável antes de ser execultado.") | |
} else { | |
fmt.Println("Deu erro") | |
} | |
} | |
func UsandoFor() { | |
for j := 0; j <= 5; j++ { | |
fmt.Println(j) | |
} | |
} | |
func PrimeiroExemploSwitch(n float64) { | |
var nota = int(n) | |
switch nota { | |
case 10, 9: | |
fmt.Println("A") | |
case 8, 7: | |
fmt.Println("B") | |
case 6, 5: | |
fmt.Println("C") | |
case 4, 3: | |
fmt.Println("D") | |
case 2, 1: | |
fmt.Println("E") | |
default: | |
fmt.Println("Nota Invalida") | |
} | |
} | |
func SegundoExemploSwitch() { | |
t := time.Now(); | |
switch { | |
case t.Hour() < 12: | |
fmt.Println("Bom dia") | |
case t.Hour() < 18: | |
fmt.Println("Boa tarde") | |
default: | |
fmt.Println("Boa Noite") | |
} | |
} | |
func TerceiroExemploSwitch(i interface{}) { | |
switch i.(type) { | |
case int: | |
fmt.Println("Inteiro") | |
case float32, float64: | |
fmt.Println("Valor Real") | |
case string: | |
fmt.Println("String") | |
case func(): | |
fmt.Println("Função") | |
default: | |
fmt.Println("Não reconhecido"); | |
} | |
} | |
func PrimeiroExemploArray() { | |
var notas[3]float64 | |
notas[0], notas[1], notas[2] = 5.6, 5.8, 5.9 | |
total := 0.0 | |
for i := 0; i < len(notas); i++ { | |
total += notas[i] | |
} | |
media := total / float64(len(notas)) | |
fmt.Printf("Média %.2f\n", media) | |
} | |
func UsandoArray_Range() { | |
numeros := [...]int{5, 6, 8, 9, 10, 17, 13} | |
for indice, valor := range numeros { | |
fmt.Printf("%d) %d\n", indice, valor) | |
} | |
for _, valor := range numeros { | |
fmt.Printf("%d\n", valor) | |
} | |
} | |
func UsandoSlice() { | |
a1 := [5]int{1, 2, 3, 4, 5} | |
s1 := a1[1:3] | |
fmt.Println("Informando a posição inicial e a final", s1) | |
s2 := a1[:4] | |
fmt.Println("Informando a posição final", s2) | |
s3 := s1[0:2] | |
fmt.Println("Informando a posição inicial e a final do primeiro Slice", s3) | |
} | |
func UsandoSlice_Make() { | |
s1 := make([]int, 5, 10) | |
s1[0] = 1 | |
s1[1] = 2 | |
s1[2] = 3 | |
s1[3] = 4 | |
s1[4] = 5 | |
s1 = append(s1, 6, 7, 8, 9, 10) | |
fmt.Println("Depois de serem atribuidos os valores", s1) | |
} | |
func UsandoSlice_Make_Copy() { | |
c1 := make([]int, 2) | |
s1 := [5]int{1, 2, 3, 4, 5} | |
copy(c1, s1[1:3]) // 2, 3 | |
fmt.Println("Copiou os elementos 2 e 3 do s1", c1) | |
} | |
func UsandoArray_Map() { | |
pessoa := make(map[int]string) | |
pessoa[123456789] = "Renan" | |
pessoa[987654321] = "Allyssa" | |
pessoa[753159852] = "Aidan" | |
pessoa[486247391] = "Fulana" | |
for indice, nome := range pessoa { | |
fmt.Println(indice, nome) | |
} | |
delete(pessoa,486247391) | |
for indice, nome := range pessoa { | |
fmt.Println(indice, nome) | |
} | |
} | |
func UsandoArray_Map_Tipo_Json() { | |
clientes := map[string]map[string]string { | |
"cod_01": { | |
"nome": "Renan Augusto", | |
"nasc": "08/02/1989", | |
"cidade": "Cerqueira César", | |
}, | |
"cod_02":{ | |
"nome": "Allyssa de Paula", | |
"nasc": "04/11/2015", | |
"cidade": "Bauru", | |
}, | |
} | |
for _, cliente := range clientes { | |
for _, nome := range cliente { | |
fmt.Println(nome) | |
} | |
} | |
} | |
func Retorno_Personalizado(nome, sobrenome string) (nome_completo string) { | |
nome_completo = (nome + " " + sobrenome); | |
return | |
} | |
func Usando_Funcao_Parametro(soma func(int, int) int, num1, num2 int) { | |
fmt.Println(soma(num1, num2)) | |
} | |
func Usando_Funcao_Valores_Dinamicos(notas ... float64) { | |
total := 0.0 | |
for _, num := range notas { | |
total += num | |
} | |
quantidadeNotas := float64(len(notas)) | |
fmt.Println("A média final é de: ", (total / quantidadeNotas)) | |
// Funcão aceita infinitos números de paramentros. | |
} | |
func Usando_Funcao_Valores_Dinamicos_Com_Slice(aprovados ...string) { | |
fmt.Println("Lista de Aprovados") | |
for i, aprovado := range aprovados { | |
fmt.Printf("%d) %s\n", i + 1, aprovado) | |
} | |
} | |
func main() { | |
UsandoIf_Else() | |
UsandoFor() | |
PrimeiroExemploSwitch(10) | |
SegundoExemploSwitch() | |
TerceiroExemploSwitch(1) | |
PrimeiroExemploArray() | |
UsandoArray_Range() | |
UsandoSlice() | |
UsandoSlice_Make() | |
UsandoSlice_Make_Copy() | |
UsandoArray_Map() | |
UsandoArray_Map_Tipo_Json() | |
fmt.Println(Retorno_Personalizado("Renan", "Augusto")) | |
Usando_Funcao_Parametro(Funcao_Modelo, 5, 6) | |
Usando_Funcao_Valores_Dinamicos(1.0, 2.0, 3.0, 4.0, 5.0) | |
aprovados := []string{"Renan", "Allyssa", "Aidan"} | |
Usando_Funcao_Valores_Dinamicos_Com_Slice(aprovados...) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment