Golang declaración explícita de variables en 5 min.

in #programming7 years ago (edited)

Declaración explícita de variables

Una declaración explícita se hace con la palabra reservada var, el nombre de la variable y el tipo de dato que va a guardar.

    var nombre string
    var edad int
    var calificacion float64 
    var aprobado bool

Cuando se declaran variables se inicializan en “cero”, por ejemplo para las variables de tipo string quedan con una cadena vacía, para las variables de tipo int quedan con valor de 0, para las variables de tipo float64 quedan con un valor de 0 y para las variables de tipo bool quedan con un valor de false.

Ejemplo: Declaración explícita de variables

otra opción de declaración de variables es la:

Declaración múltiple de variables

En la declaración múltiple solamente se usa una palabra reservada var y se agrupan los nombres con sus respectivos tipos dentro de paréntesis:

    var (
        nombre string
        edad int
        calificacion float64 
        aprobado bool
    )

Ejemplo: Declaración múltiple de variables

Las declaraciones implícitas y las declaraciones implícitas múltiples, pueden tener alcance dentro y fuera de las funciones, o dicho de otra manera, las puedo poner dentro o fuera de las funciones, en los ejemplos anteriores están dentro de la función main.
Y en los siguiente ejemplos están fuera de la función main.

Ejemplo: Declaración explicita de variables, fuera de la función

Ejemplo: Declaración multiple de variables, fuera de la función

Otra opción es poner las declaraciones fuera y después de la función main

Otro Ejemplo: Declaración múltiple de variables, fuera de la función

No es común hacer la declaración de variables después de las funciones pero es importante observar qué funciona.

Lo que no funciona es lo siguiente:

/*
twitter@hector_gool
*/

package main

import (
    "fmt"
)

func main() {

    fmt.Printf("Nombre: %v, Edad: %v, Calificación: %v, Aprobado: %v", nombre, edad, calificacion, aprobado)

    var (
        nombre       string
        edad         int
        calificacion float64
        aprobado     bool
    )

}

Al ejecutar el programa voy a obtener algo como lo siguiente:

tmp/sandbox723369642/main.go:13:70: undefined: nombre
tmp/sandbox723369642/main.go:13:78: undefined: edad
tmp/sandbox723369642/main.go:13:84: undefined: calificacion
tmp/sandbox723369642/main.go:13:98: undefined: aprobado

La razón por la que no funciona es por qué estoy queriendo imprimir unas variables qué en ese punto del programa no han sido declaradas o definidas. Primero tienen qué ser declaradas para que puedan ser usadas.

Ejemplo: esto no se debe hacer

En el siguiente post hablaré de la asignación de valores a variables y todos esos pequeños detalles que tienen.

Apreciaria tu voto si te gusto el post o un follow para que te mantengas al día de estos post o cualquier comentario para hacerme saber lo qué opinas.
Hasta pronto.

Sort:  

Congratulations @elsanto! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

You got your First payout
Award for the total payout received

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Congratulations @elsanto! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Congratulations @elsanto! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Congratulations @elsanto! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Congratulations @elsanto! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Coin Marketplace

STEEM 0.23
TRX 0.11
JST 0.029
BTC 66255.12
ETH 3564.10
USDT 1.00
SBD 3.15