mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-04-27 07:33:57 +00:00
Half done through the translation
I need moar time gosh >.<
This commit is contained in:
parent
b392e48e71
commit
38ff402322
@ -48,7 +48,8 @@ func main() {
|
|||||||
// Las funciones llevan parámetros entre paréntesis.
|
// Las funciones llevan parámetros entre paréntesis.
|
||||||
// Si no hay parámetros, los paréntesis siguen siendo obligatorios.
|
// Si no hay parámetros, los paréntesis siguen siendo obligatorios.
|
||||||
func beyondHello() {
|
func beyondHello() {
|
||||||
var x int // Declaración de una variable. Las variables se deben declarar antes de // utilizarlas.
|
var x int // Declaración de una variable. Las variables se deben declarar antes de
|
||||||
|
// utilizarlas.
|
||||||
x = 3 // Asignación de variables.
|
x = 3 // Asignación de variables.
|
||||||
// Declaración "corta" con := para inferir el tipo, declarar y asignar.
|
// Declaración "corta" con := para inferir el tipo, declarar y asignar.
|
||||||
y := 4
|
y := 4
|
||||||
@ -92,33 +93,33 @@ saltos de línea.` // mismo tipo cadena
|
|||||||
var d2 [][]float64 // solo declaración, sin asignación
|
var d2 [][]float64 // solo declaración, sin asignación
|
||||||
bs := []byte("a slice") // sintaxis de conversión de tipo
|
bs := []byte("a slice") // sintaxis de conversión de tipo
|
||||||
|
|
||||||
p, q := learnMemory() // declares p, q to be type pointer to int.
|
p, q := learnMemory() // declara p, q para ser un tipo puntero a int.
|
||||||
fmt.Println(*p, *q) // * follows a pointer. This prints two ints.
|
fmt.Println(*p, *q) // * sigue un puntero. Esto imprime dos ints.
|
||||||
|
|
||||||
// Maps are a dynamically growable associative array type, like the
|
// Los Maps son arrays asociativos dinámicos, como los hash o diccionarios
|
||||||
// hash or dictionary types of some other languages.
|
// de otros lenguajes
|
||||||
m := map[string]int{"three": 3, "four": 4}
|
m := map[string]int{"three": 3, "four": 4}
|
||||||
m["one"] = 1
|
m["one"] = 1
|
||||||
|
|
||||||
// Unused variables are an error in Go.
|
// Las variables no utilizadas en Go producen error.
|
||||||
// The underbar lets you "use" a variable but discard its value.
|
// El guión bajo permite "utilizar" una variable, pero descartar su valor.
|
||||||
_, _, _, _, _, _, _, _, _ = s2, g, f, u, pi, n, a3, s4, bs
|
_, _, _, _, _, _, _, _, _ = s2, g, f, u, pi, n, a3, s4, bs
|
||||||
// Output of course counts as using a variable.
|
// Esto cuenta como utilización de variables.
|
||||||
fmt.Println(s, c, a4, s3, d2, m)
|
fmt.Println(s, c, a4, s3, d2, m)
|
||||||
|
|
||||||
learnFlowControl() // back in the flow
|
learnFlowControl() // vuelta al flujo
|
||||||
}
|
}
|
||||||
|
|
||||||
// Go is fully garbage collected. It has pointers but no pointer arithmetic.
|
// Go posee recolector de basura. Tiene puntero pero no aritmética de punteros.
|
||||||
// You can make a mistake with a nil pointer, but not by incrementing a pointer.
|
// Puedes cometer un errores con un puntero nil, pero no incrementando un puntero.
|
||||||
func learnMemory() (p, q *int) {
|
func learnMemory() (p, q *int) {
|
||||||
// Named return values p and q have type pointer to int.
|
// q y p tienen un tipo puntero a int.
|
||||||
p = new(int) // built-in function new allocates memory.
|
p = new(int) // función incorporada que asigna memoria.
|
||||||
// The allocated int is initialized to 0, p is no longer nil.
|
// La asignación de int se inicializa a 0, p ya no es nil.
|
||||||
s := make([]int, 20) // allocate 20 ints as a single block of memory
|
s := make([]int, 20) // asigna 20 ints a un solo bloque de memoria.
|
||||||
s[3] = 7 // assign one of them
|
s[3] = 7 // asignar uno de ellos
|
||||||
r := -2 // declare another local variable
|
r := -2 // declarar otra variable local
|
||||||
return &s[3], &r // & takes the address of an object.
|
return &s[3], &r // & toma la dirección de un objeto.
|
||||||
}
|
}
|
||||||
|
|
||||||
func expensiveComputation() int {
|
func expensiveComputation() int {
|
||||||
@ -126,69 +127,69 @@ func expensiveComputation() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func learnFlowControl() {
|
func learnFlowControl() {
|
||||||
// If statements require brace brackets, and do not require parens.
|
// La declaración If requiere llaves, pero no paréntesis.
|
||||||
if true {
|
if true {
|
||||||
fmt.Println("told ya")
|
fmt.Println("told ya")
|
||||||
}
|
}
|
||||||
// Formatting is standardized by the command line command "go fmt."
|
// El formato está estandarizado por el comando "go fmt."
|
||||||
if false {
|
if false {
|
||||||
// pout
|
// pout
|
||||||
} else {
|
} else {
|
||||||
// gloat
|
// gloat
|
||||||
}
|
}
|
||||||
// Use switch in preference to chained if statements.
|
// Utiliza switch preferiblemente para if encadenados.
|
||||||
x := 1
|
x := 1
|
||||||
switch x {
|
switch x {
|
||||||
case 0:
|
case 0:
|
||||||
case 1:
|
case 1:
|
||||||
// cases don't "fall through"
|
// los cases no se mezclan, no requieren de "break"
|
||||||
case 2:
|
case 2:
|
||||||
// unreached
|
// no llega
|
||||||
}
|
}
|
||||||
// Like if, for doesn't use parens either.
|
// Como if, for no utiliza paréntesis tampoco.
|
||||||
for x := 0; x < 3; x++ { // ++ is a statement
|
for x := 0; x < 3; x++ { // ++ es una sentencia
|
||||||
fmt.Println("iteration", x)
|
fmt.Println("iteration", x)
|
||||||
}
|
}
|
||||||
// x == 1 here.
|
// x == 1 aqui.
|
||||||
|
|
||||||
// For is the only loop statement in Go, but it has alternate forms.
|
// For es la única sentencia de bucle en Go, pero tiene formas alternativas.
|
||||||
for { // infinite loop
|
for { // bucle infinito
|
||||||
break // just kidding
|
break // solo bromeaba!
|
||||||
continue // unreached
|
continue // no llega
|
||||||
}
|
}
|
||||||
// As with for, := in an if statement means to declare and assign y first,
|
// Como en for, := en una sentencia if significa declarar y asignar primero,
|
||||||
// then test y > x.
|
// luego comprobar y > x.
|
||||||
if y := expensiveComputation(); y > x {
|
if y := expensiveComputation(); y > x {
|
||||||
x = y
|
x = y
|
||||||
}
|
}
|
||||||
// Function literals are closures.
|
// Los literales de funciones son "closures".
|
||||||
xBig := func() bool {
|
xBig := func() bool {
|
||||||
return x > 100 // references x declared above switch statement.
|
return x > 100 // referencia a x declarada encima de la sentencia switch.
|
||||||
}
|
}
|
||||||
fmt.Println("xBig:", xBig()) // true (we last assigned 1e6 to x)
|
fmt.Println("xBig:", xBig()) // verdadero (la última vez asignamos 1e6 a x)
|
||||||
x /= 1e5 // this makes it == 10
|
x /= 1e5 // esto lo hace == 10
|
||||||
fmt.Println("xBig:", xBig()) // false now
|
fmt.Println("xBig:", xBig()) // ahora es falso
|
||||||
|
|
||||||
// When you need it, you'll love it.
|
// Cuando lo necesites, te encantará.
|
||||||
goto love
|
goto love
|
||||||
love:
|
love:
|
||||||
|
|
||||||
learnInterfaces() // Good stuff coming up!
|
learnInterfaces() // Buen material dentro de poco!
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define Stringer as an interface type with one method, String.
|
// Define Stringer como un tipo interfaz con un método, String.
|
||||||
type Stringer interface {
|
type Stringer interface {
|
||||||
String() string
|
String() string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define pair as a struct with two fields, ints named x and y.
|
// Define pair como un struct con dos campos int, x e y.
|
||||||
type pair struct {
|
type pair struct {
|
||||||
x, y int
|
x, y int
|
||||||
}
|
}
|
||||||
|
|
||||||
// Define a method on type pair. Pair now implements Stringer.
|
// Define un método del tipo pair. Pair ahora implementa Stringer.
|
||||||
func (p pair) String() string { // p is called the "receiver"
|
func (p pair) String() string { // p se llama "recibidor"
|
||||||
// Sprintf is another public function in package fmt.
|
// Sprintf es otra función pública del paquete fmt.
|
||||||
// Dot syntax references fields of p.
|
// Dot syntax references fields of p.
|
||||||
return fmt.Sprintf("(%d, %d)", p.x, p.y)
|
return fmt.Sprintf("(%d, %d)", p.x, p.y)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user