Documentos de Académico
Documentos de Profesional
Documentos de Cultura
9 de Noviembre de 2011
p a c k a g e main
i m p o r t " fmt "
f u n c main ( ) {
fmt . P r i n t f ( " H e l l o w o r l d " )
}
var x , y , z i n t
i , j = j , i // i n t e r c a m b i a i con j
f u n c add ( x i n t , y i n t ) i n t {
return x + y
}
f u n c add ( x , y i n t ) i n t {
return x + y
}
f u n c swap ( x , y s t r i n g ) ( s t r i n g , s t r i n g ) {
return y , x
}
x := 1
y , z := 2 , 3
const Pi f l o a t 6 4 = 3.14
c o n s t c a d e n a = " Hola mundo"
f o r i := 0 ; i < 1 0 ; i++ {
sum += i
}
f o r sum < 1000 { // w h i l e
sum += sum
}
f o r { // i n f i n i t o
}
x = 10
if x < 0 {
fmt . P r i n t l n ( " x e s menor que 0 " )
}
i f v := 5 ; x < v {
fmt . P r i n t l n ( " x e s menor que v " )
}
func f () {
f o r i := 0 ; i < 5 ; i++ {
d e f e r fmt . P r i n t f (" %d " , i )
}
}
type Vertex s t r u c t {
X int
Y int
}
v := V e r t e x { 1 , 2}
v .X = 4
var (
p = V e r t e x { 1 , 2} // tipo Vertex
q = &V e r t e x { 1 , 2} // tipo Vertex
r = V e r t e x {X : 1} // Y:0 es i m p l i c i t o
s = V e r t e x {} // X : 0 and Y : 0
)
type Vertex s t r u c t {
X, Y i n t
}
f u n c main ( ) {
v a r v V e r t e x = new ( V e r t e x )
v := new ( V e r t e x )
}
var intArray [ 3 ] i n t
i n t A r r a y 2 := [ 3 ] i n t { 1 , 2 , 3}
i n t A r r a y 3 := [ . . . ] i n t { 4 , 5 , 6}
func f ( a [ 3 ] i n t ) { a [0]=10 }
func fp ( a [ 3 ] i n t ) { a [0]=10 }
f u n c main ( ) {
var ar [ 3 ] i n t
f ( ar )
fmt . P r i n t l n ( a r )
f p (& a r )
fmt . P r i n t l n ( a r )
}
func reverseArray ( a [ 1 0 ] i n t ) [ 1 0 ] i n t {
f o r i , j := 0 , l e n ( a ) 1; i < j ; i , j = i +1, j 1 {
a[ i ] , a[ j ] = a[ j ] , a[ i ]
}
return a
}
var arr [ 3 ] i n t
a r r = [ 3 ] i n t {1 ,2 ,3}
var s l i c e [ ] i n t
s l i c e = arr [0:2]
v a r v [ ] i n t = make ( [ ] i n t , 1 0 0 )
p := make ( [ ] i n t , 1 0 0 )
m = make ( map [ s t r i n g ] V e r t e x )
m[ " B e l l Labs " ] = V e r t e x {
40.68433 , 74.39967 ,
}
fmt . P r i n t l n (m[ " B e l l Labs " ] )
func f i b () func () i n t {
a , b := 0 , 1
return func () i n t {
a , b = b , a + b
return a
}
}
f := f i b ( )
f o r i := 0 ; i < 10 ; i ++ {
fmt . P r i n t l n ( f ( ) )
}
I Un ciclo for (de tipo range) puede usarse para iterar sobre un
slice o un map
I Se puede ignorar la llave o el valor usando _
v a r pow = [ ] i n t { 1 , 2 , 4 , 8 , 1 6 , 3 2 , 6 4 , 128}
f o r i , v := r a n g e pow {
fmt . P r i n t f ("2 %d = % d\n " , i , v )
}
f o r _, v := r a n g e pow {
fmt . P r i n t f (" %d\n " , v a l u e )
}
s w i t c h o s := r u n t i m e . GOOS ; o s {
case " darwin ":
fmt . P r i n t l n ( "OS X . " )
case " l i n u x ":
fmt . P r i n t l n ( " L i n u x . " )
default :
fmt . P r i n t f (" %s . " , o s )
}
type Vertex s t r u c t {
X, Y f l o a t 6 4
}
f u n c ( p V e r t e x ) Sum ( ) f l o a t 6 4 {
return p .X + p .Y
}
f u n c main ( ) {
p := new ( V e r t e x )
p . X = 3 ; p . Y=4
fmt . P r i n t l n ( p . Sum ( ) )
}
type Vertex s t r u c t {
X, Y f l o a t 6 4
}
f u n c ( p V e r t e x ) Sum ( ) f l o a t 6 4 {
return p .X + p .Y
}
f u n c main ( ) {
p := new ( V e r t e x )
p . X = 3 ; p . Y=4
fmt . P r i n t l n ( p . Sum ( ) )
}
ch := make ( chan i n t ) // un c a n a l de e n t e r o s
ch < v // e n v i a r v a l c a n a l ch
v := < ch // r e c i b e d e l c a n a l ch y a s i g n a e l v a l o r
ch := make ( chan i n t , 1 0 0 )
f o r i := r a n g e ch