Open vkorbes opened 4 years ago
Acho legal esses desafios, fiquei atento já que uso linux e a execução é case sensitive e sem querer deixei a variavel Y em maisculo em uma parte e outra não.
package main
import (
"fmt"
)
//tipo criado
type laranjas int8
//criado variavel global
var x laranjas
var y int8
func main (){
fmt.Println("O valor de X: ",x)
fmt.Printf("O o tipo da variavel X: %T \n", x)
x =42
fmt.Println("O valor de X: ",x)
y = int8(x)
fmt.Println("O valor de Y: ",y)
fmt.Printf("O o tipo da variavel Y: %T \n", y)
}
Código a seguir:
package main
import "fmt"
type modelo int
var x modelo var y int
func main() {
fmt.Printf("%v, %T\n", x, x)
x = 42
fmt.Printf("Valor da variável x, após a atribuição: %v\n", x)
y = int(x)
fmt.Printf("Valor e tipo da variável y, após a atribuição: %v, %T\n", y, y)
}
🚀
package main
import (
"fmt"
)
type numeros int
var x numeros
var y int
func main() {
fmt.Printf("O valor de X é %d do tipo %T\n", x, x)
x = 42
fmt.Printf("Agora o valor de X é %d do tipo %T\n", x, x)
y = int(x)
fmt.Printf("O valor de y é %d do tipo %T\n", y, y)
}
package main
import "fmt"
type testetipo int
var (
x testetipo
y int
)
func main() {
fmt.Printf("O valor de x é: %v\n", x)
fmt.Printf("O tipo de x é: %T\n\n", x)
x = 42
fmt.Printf("Agora o valor de x é: %v\n", x)
y = int(x)
fmt.Printf("\nO valor de y é: %v\n", y)
fmt.Printf("O tipo de y é: %T\n", y)
}
package main
import "fmt"
// Cria um tipo onde o tipo subjacente é int
type exemploTipo int
// Cria uma variável com o tipo criado acima
var x exemploTipo
// Cria uma variável com o tipo subjacente do exemploTipo
var y int
func main() {
// Imprime o valor da variável
fmt.Println(x)
// Imprime o tipo da variável
fmt.Printf("%T\n", x)
// Atribui o valor 42 a variável
x = 42
// Imprime novamente o valor da variável
fmt.Println(x)
// Converte o tipo da variável x para int
y = int(x)
// Imprime o valor da variável
fmt.Println(y)
// Imprime o tipo da variável
fmt.Printf("%T", y)
}
Output:
0
main.exemploTipo
42
42
int
package main
import "fmt"
type meonly int
var x meonly
var y int
func main() {
fmt.Printf("%v typeof %T\n", x, x)
x = 42
fmt.Printf("%v typeof %T\n", x, x)
fmt.Printf("%v typeof %T\n", y, y)
y = int(x)
fmt.Printf("%v typeof %T\n", y, y)
}
package main
import "fmt"
type cyber int
var x cyber
var y int
func main(){
fmt.Printf("%v %T\n", x, x)
x = 42
fmt.Printf("%v\n", x)
y = int(x)
fmt.Printf("%v %T\n", y, y)
}
package main
import (
"fmt"
)
type bilada int
var x bilada
var y int
func main() {
fmt.Printf("Variável: x | Tipo: %T | Biladas(valor): %v \n", x, x)
x = 42
fmt.Printf("Variável: x | Tipo: %T | Biladas(valor): %v \n", x, x)
fmt.Printf("Variável: y | Tipo: %T | Valor: %v \n", y, y)
y = int(x)
fmt.Printf("Variável: y | Tipo: %T | Valor: %v \n", y, y)
}
Output:
Variável: x | Tipo: main.bilada | Biladas(valor): 0
Variável: x | Tipo: main.bilada | Biladas(valor): 42
Variável: y | Tipo: int | Valor: 0
Variável: y | Tipo: int | Valor: 42
package main
import ( "fmt" )
type cart int
var x cart
var y int
func main() { fmt.Println("Valor de x:",x) fmt.Printf("Tipo de x :%T\n",x) x = 42 fmt.Println("Novo valor de x :",x) y = int(x) fmt.Println("Valor de y:",y) fmt.Printf("Tipo de y : %T\n",y) }
### Saída
Valor de x: 0 Tipo de x :main.cart Novo valor de x : 42 Valor de y: 42 Tipo de y : int
package main
import (
"fmt"
)
// Criação subtipo int
type subtype int
// Criação da variável x com palavra-chave "var"
var x subtype
var y int
func main() {
fmt.Px = int(x)rintln("Valor x:", x)
fmt.Printf("Tipo x: %T\n", x)
x = 42
fmt.Println("Valor x:", x)
y = int(x)
fmt.Println("Valor y:", y)
fmt.Printf("Tipo y: %T\n", y)
}
[O que foi difícil?] Esqueci que tipo tipo subjacente é o tipo "primário", não o derivado.
package main
import "fmt"
type bora int
var x bora var y int
func main() { fmt.Printf("Valor inicial de X: %v\n", x) fmt.Printf("Tipo de X: %T\n", x) x = 42 fmt.Printf("Valor final de X após atribuição: %v\n", x)
y = int(x)
fmt.Printf("Tipo de Y: %T\n", y)
fmt.Printf("Valor de Y: %v\n", y)
}
Code:
// Main Package
package main
// Import of the packages
import (
"fmt"
)
// Package-level scope variables
var (
y int
)
// Typings
type hotdog int
// Main function
func main() {
// Code-scope variables
var (
x hotdog
)
// Show the value of the variable "X"
fmt.Printf("Value of X: %v\n", x)
// Show the type of variable "X"
fmt.Printf("Type of X: %T\n", x)
// Assign 42 to the variable "X" using the "=" operator
x = 42
// Show the value of the variable "X"
fmt.Printf("Value of X: %v\n\n", x)
// Converting the type of X to Y
y = int(x)
// Show the value of the variable "Y"
fmt.Printf("Value of Y: %v\n", y)
// Show the type of variable "Y"
fmt.Printf("Type of Y: %T", y)
}
Output:
Value of X: 0
Type of X: main.hotdog
Value of X: 42
Value of Y: 42
Type of Y: int
package main
import "fmt"
type abrindo int
var x abrindo var y int
func main() {
fmt.Printf("%v\t%T\t\n", x, x)
x = 42
fmt.Println(x)
y = int(x)
fmt.Printf("%v\t%T\t\n", y, y)
}
type inteiro int type segundointeiro inteiro
var x inteiro var y int
func main() { fmt.Printf("%d\t%T\n", x, x) x = 47 fmt.Println(x)
y = int(x)
fmt.Printf("%d\t%T\n", y, y)
}
package main
import ( "fmt" )
type hdo int var x hdo var y int
func main() {
fmt.Printf("%v = %T\n", x, x)
x = 42
fmt.Println(x)
y := int(x)
fmt.Printf("%v = %T\n", y, y)
}
https://go.dev/play/p/W5SbmaZqo_U
// You can edit this code!
// Click here and start typing.
package main
import "fmt"
type anak int
var x anak
var y anak
func main() {
fmt.Println(x)
fmt.Printf("%T\n", x)
x = 42
fmt.Printf("%v\n", x)
y = anak(x)
fmt.Printf("%v", y)
fmt.Printf("%T\n", y)
}
-----------------------------------------------------------------------------------------------------------------------------------------------
Output
0
main.anak
42
42
main.anak
Program exited.
-----------------------------------------------------------------------------------------------------------------------------------------------
/*
- Utilizando a solução do exercício anterior:
1. Em package-level scope, utilizando a palavra-chave var, crie uma variável com o identificador "y". O tipo desta variável deve ser o tipo subjacente do tipo que você criou no exercício anterior.
2. Na função main:
1. Isto já deve estar feito:
1. Demonstre o valor da variável "x"
2. Demonstre o tipo da variável "x"
3. Atribua 42 à variável "x" utilizando o operador "="
4. Demonstre o valor da variável "x"
2. Agora faça tambem:
1. Utilize conversão para transformar o tipo do valor da variável "x" em seu tipo subjacente e, utilizando o operador "=", atribua o valor de "x" a "y"
2. Demonstre o valor de "y"
3. Demonstre o tipo de "y"
*/
package main
import (
"fmt"
)
type algumTipo int
var x algumTipo
var y int
func main() {
fmt.Printf("%v", x)
fmt.Printf("\n%T", x)
x = 42
fmt.Printf("\n%v", x)
y = int(x)
fmt.Printf("\n\n%v", y)
fmt.Printf("\n%T", y)
}
package main
import "fmt"
type xpto int
var x xpto
var y int
func main() {
fmt.Printf("%v\n", x)
fmt.Printf("Tipo da variavel x: %T\n", x)
x = 42
fmt.Println("Valor de x:", x)
y = int(x)
fmt.Println("Valor de y:", y)
fmt.Printf("Tipo da variavel y: %T", y)
}
package main
import "fmt"
type newType int
var x newType
var y int
func main() {
fmt.Printf("Valor de x=%v\n", x)
fmt.Printf("Tipo de x=%T\n", x)
x = 42
fmt.Printf("Valor de x atualizado=%v\n", x)
y = int(x)
fmt.Printf("Valor de y=%v\n", y)
fmt.Printf("Tipo de y=%T\n", y)
}
Exercício: Capítulo 3, Exercício 5 (Nível: 1)
Use esta thread para compartilhar sua solução, discutir o exercício com os colegas e pedir ajuda caso tenha dificuldades!