p e n s o u r c e p r o g r a m m i n g l a n g u a g e t h a t m a k e s i t e a s y t o b u i l d s i m p l e , r e l i a b l e , a n d e f f i c i e n t s o f t w a r e . - h t t p : / / g o l a n g . o r g
calculatedPi function is visible only inside package. p a c k a g e m a i n i m p o r t " f m t " i m p o r t " m a t h " c o n s t L i m i t = 1 0 0 0 0 0 0 0 0 v a r l i m i t = 1 0 0 0 0 0 0 0 0 f u n c m a i n ( ) { f m t . P r i n t l n ( " P I : \ n C a l c u l a t e d " , V a l u e O f P i ( ) , " \ n C o n s t a n t " , m a t h . P i ) } f u n c V a l u e O f P i ( ) f l o a t 6 4 { r e t u r n c a l c u l a t e d P i ( L i m i t ) } f u n c c a l c u l a t e d P i ( l i m i t f l o a t 6 4 ) f l o a t 6 4 { Run
l a t e d P i ( l i m i t f l o a t 6 4 ) f l o a t 6 4 { v a r s u m f l o a t 6 4 v a r n u m , t o g g l e = 4 . 0 , t r u e f o r i : = 1 . 0 ; i < l i m i t ; i + = 2 { i f t o g g l e { s u m + = n u m / i } e l s e { s u m - = n u m / i } t o g g l e = ! t o g g l e } r e t u r n s u m } Run
i n i m p o r t " f m t " v a r a g e L i m i t = 6 0 f u n c m a i n ( ) { v a r a g e = 3 6 i f o v e r l i m i t ( a g e ) { f m t . P r i n t l n ( a g e , " i s o v e r l i m i t " ) } e l s e { f m t . P r i n t l n ( a g e , " i s u n d e r l i m i t " ) } } f u n c o v e r l i m i t ( a g e i n t ) b o o l { r e t u r n a g e > a g e L i m i t } Run
) { v a r l o o p b o o l v a r i t e m s = [ ] s t r i n g { " O n e " , " T w o " , " T h r e e " } f o r i , i t e m : = r a n g e i t e m s { f m t . P r i n t l n ( i , i t e m ) } f o r i : = 0 ; i < l e n ( i t e m s ) ; i + + { f m t . P r i n t l n ( i , i t e m s [ i ] ) } f o r l o o p { f m t . P r i n t l n ( " N e v e r r e a c h e d " ) } f o r { / / i n f i n i t e l o o p } } Run
) { f m t . P r i n t l n ( g e t A g e ( ) ) f m t . P r i n t l n ( g e t N a m e A n d A g e ( ) ) f m t . P r i n t l n ( b e t t e r N a m e A n d A g e ( ) ) } f u n c g e t A g e ( ) i n t { r e t u r n 3 3 } f u n c g e t N a m e A n d A g e ( ) ( n a m e s t r i n g , a g e i n t ) { n a m e = " J o h n " a g e = g e t A g e ( ) r e t u r n } f u n c b e t t e r N a m e A n d A g e ( ) ( s t r i n g , i n t ) { n a m e : = " J o h n " r e t u r n n a m e , g e t A g e ( ) } Run
r u c t { t i m e . D u r a t i o n } t y p e P e r s o n s t r u c t { f i r s t N a m e , l a s t N a m e s t r i n g o c c u p a t i o n s t r i n g a g e A g e } f u n c m a i n ( ) { p e r s o n : = P e r s o n { } p e r s o n . f i r s t N a m e = " V i j a y " p e r s o n . l a s t N a m e = " D e e n a n a t h C h a u h a n " p e r s o n . a g e = A g e { 3 6 * 3 6 5 * 2 4 * t i m e . H o u r } f m t . P r i n t l n ( p e r s o n . f i r s t N a m e , p e r s o n . l a s t N a m e , " i s " , p e r s o n . a g e , " o l d . " ) } Run
s o n ) S a y N a m e ( ) s t r i n g { r e t u r n f m t . S p r i n t f ( " M y N a m e i s % s \ n . . . % s % s " , t . l a s t N a m e , t . f i r s t N a m e , t . l a s t N a m e , ) } f u n c m a i n ( ) { p e r s o n : = P e r s o n { } p e r s o n . f i r s t N a m e = " J a m e s " p e r s o n . l a s t N a m e = " B o n d " f m t . P r i n t l n ( p e r s o n . S a y N a m e ( ) ) } Run
n t ) S t r i n g ( ) s t r i n g { r e t u r n f m t . S p r i n t f ( " M y N a m e i s % s \ n . . . % s % s " , t . l a s t N a m e , t . f i r s t N a m e , t . l a s t N a m e , ) } f u n c m a i n ( ) { a g e n t : = A g e n t { } a g e n t . f i r s t N a m e = " J a m e s " a g e n t . l a s t N a m e = " B o n d " f m t . P r i n t l n ( a g e n t ) } Run
a i n i m p o r t " f m t " t y p e A s t r u c t { n a m e s t r i n g a g e f l o a t 6 4 } f u n c m a i n ( ) { v a r s t r s t r i n g v a r n u m i n t 6 4 v a r a A f m t . P r i n t f ( " % # v , \ n % # v , \ n % # v \ n " , s t r , n u m , a ) } Run
n t t y p e W o o d s t r u c t { M a s s } t y p e P a p e r s t r u c t { M a s s } t y p e B u r n e r i n t e r f a c e { C a n B u r n ( ) b o o l B u r n T i m e ( ) M a s s }
s s ) C a n B u r n ( ) b o o l { r e t u r n m > 0 } f u n c ( w W o o d ) B u r n T i m e ( ) M a s s { r e t u r n 1 0 * w . M a s s } f u n c ( p P a p e r ) B u r n T i m e ( ) M a s s { r e t u r n 1 * p . M a s s } f u n c B u r n ( b B u r n e r ) { i f b . C a n B u r n ( ) { f m t . P r i n t f ( " % T w i l l b u r n f o r % d s e c o n d s \ n " , b , b . B u r n T i m e ( ) ) } e l s e { f m t . P r i n t f ( " % T c a n ' t b u r n \ n " , b ) } } f u n c m a i n ( ) { B u r n ( W o o d { 1 0 } ) B u r n ( P a p e r { 1 0 } ) B u r n ( P a p e r { 0 } ) } / / Run
i n i m p o r t " f m t " f u n c m a i n ( ) { v a r a i n t a = 3 f m t . P r i n t l n ( a , & a ) f m t . P r i n t f ( " % T % T \ n " , a , & a ) v a r b i n t v a r p * i n t f m t . P r i n t l n ( b , p ) p = & b f m t . P r i n t l n ( b , p ) } Run
a i n i m p o r t " f m t " t y p e P e r s o n s t r u c t { n a m e s t r i n g } f u n c m a i n ( ) { a : = P e r s o n { " A " } f m t . P r i n t l n ( a . n a m e ) a . c h a n g e N a m e ( ) f m t . P r i n t ( a . n a m e ) } f u n c ( p P e r s o n ) c h a n g e N a m e ( ) { p . n a m e = " B " } Run
i n i m p o r t " f m t " f u n c m a i n ( ) { v a r a [ ] s t r i n g a = a p p e n d ( a , " V i j a y " ) f m t . P r i n t l n ( a ) v a r b = [ ] s t r i n g { " 1 " , " 2 " , " 3 " , " 4 " , " 5 " } f m t . P r i n t l n ( b ) } Run
i n i m p o r t " f m t " f u n c m a i n ( ) { v a r a = [ 3 ] s t r i n g { " 1 " , " V i j a y " , " 3 " } v a r b = a [ 0 : 3 ] / / c r e a t e s l i c e f r o m a r r a y f m t . P r i n t l n ( b ) b = a p p e n d ( b , " 4 " ) b = a p p e n d ( b , " 5 " ) f m t . P r i n t l n ( b ) } Run
i n i m p o r t " f m t " f u n c m a i n ( ) { v a r m m a p [ s t r i n g ] f l o a t 6 4 m [ " v a l u e O f Z e r o " ] = 0 . 0 f m t . P r i n t l n ( m [ " v a l u e O f Z e r o " ] ) } Run
i n i m p o r t " f m t " t y p e W e e k d a y i n t v a r d a y s = m a k e ( m a p [ i n t ] s t r i n g ) f u n c i n i t ( ) { d a y s [ 0 ] = " S u n d a y " d a y s [ 1 ] = " M o n d a y " d a y s [ 2 ] = " T u e s d a y " } f u n c m a i n ( ) { v a r d W e e k d a y f m t . P r i n t l n ( d ) } f u n c ( d W e e k d a y ) S t r i n g ( ) s t r i n g { r e t u r n d a y s [ i n t ( d ) ] } Run
a i n i m p o r t ( " i o " " o s " ) f u n c m a i n ( ) { f i l e , e r r : = o s . O p e n ( " e r r 2 . g o " ) d i e ( e r r ) i o . C o p y ( o s . S t d o u t , f i l e ) } f u n c d i e ( e r r e r r o r ) { i f e r r ! = n i l { p a n i c ( e r r ) } } Run
m a i n i m p o r t ( " i o " " l o g " " o s " ) f u n c m a i n ( ) { f i l e , e r r : = o s . O p e n ( " e r r . g o " ) i f e r r ! = n i l { l o g . P r i n t l n ( e r r ) } i f _ , e r r : = i o . C o p y ( o s . S t d o u t , f i l e ) ; e r r ! = n i l { l o g . P r i n t l n ( e r r ) } } Run
r i n t e r f a c e { R e a d ( p [ ] b y t e ) ( n i n t , e r r e r r o r ) } t y p e W r i t e r i n t e r f a c e { W r i t e ( p [ ] b y t e ) ( n i n t , e r r e r r o r ) } t y p e R e a d e r W r i t e r i n t e r f a c e { R e a d e r W r i t e r }
o n s t r u c t { f i r s t N a m e , l a s t N a m e s t r i n g } t y p e T e a c h e r s t r u c t { P e r s o n S u b j e c t s t r i n g } f u n c m a i n ( ) { t : = T e a c h e r { } t . f i r s t N a m e = " J o h n " t . l a s t N a m e = " D o e " t . S u b j e c t = " E n g l i s h " f m t . P r i n t l n ( t ) } f u n c ( t T e a c h e r ) S t r i n g ( ) s t r i n g { r e t u r n f m t . S p r i n t f ( " % s % s ( % s ) " , t . f i r s t N a m e , t . l a s t N a m e , t . S u b j e c t ) } Run
start, run, and complete in overlapping time periods. It doesn't necessarily mean they'll ever both be running at the same instant. Parallelism is when tasks literally run at the same time.
i n ( ) { f o r n u m : = 0 ; n u m < 2 0 ; n u m + + { j o b ( n u m ) } } f u n c j o b ( n u m i n t ) { t i m e . S l e e p ( t i m e . D u r a t i o n ( n u m * 1 2 0 ) * t i m e . M i l l i s e c o n d ) f m t . P r i n t l n ( " J o b " , n u m ) } Run
i n ( ) { f o r n u m : = 0 ; n u m < 2 0 ; n u m + + { g o j o b ( n u m ) } t i m e . S l e e p ( 5 * t i m e . S e c o n d ) } f u n c j o b ( n u m i n t ) { t i m e . S l e e p ( t i m e . D u r a t i o n ( n u m * 1 2 0 ) * t i m e . M i l l i s e c o n d ) f m t . P r i n t l n ( " J o b " , n u m ) } Run
i n ( ) { v a r w g s y n c . W a i t G r o u p f o r n u m : = 0 ; n u m < 2 0 ; n u m + + { w g . A d d ( 1 ) g o j o b ( n u m , & w g ) } w g . W a i t ( ) } f u n c j o b ( n u m i n t , w g * s y n c . W a i t G r o u p ) { t i m e . S l e e p ( t i m e . D u r a t i o n ( n u m * 1 2 0 ) * t i m e . M i l l i s e c o n d ) f m t . P r i n t l n ( " J o b " , n u m ) w g . D o n e ( ) } Run
i n ( ) { c o u n t , t 1 : = 1 0 , t i m e . N o w ( ) v a r r e s u l t = m a k e ( c h a n i n t ) f o r i : = 0 ; i < c o u n t ; i + + { g o j o b ( r e s u l t ) } f o r i : = 0 ; i < c o u n t ; i + + { f m t . P r i n t l n ( " J o b " , i , " " , < - r e s u l t ) } f m t . P r i n t l n ( " T o o k " , t i m e . N o w ( ) . S u b ( t 1 ) ) } f u n c j o b ( r e s u l t c h a n i n t ) { r e s u l t < - d o J o b ( ) } Run
i n ( ) { c o u n t , t 1 : = 1 0 , t i m e . N o w ( ) d e f e r f u n c ( ) { f m t . P r i n t l n ( " T o o k " , t i m e . N o w ( ) . S u b ( t 1 ) ) } ( ) v a r r e s u l t = m a k e ( c h a n i n t ) f o r i : = 0 ; i < c o u n t ; i + + { g o j o b ( r e s u l t ) } t i m e o u t : = t i m e . N e w T i m e r ( 2 * t i m e . S e c o n d ) f o r i : = 0 ; i < c o u n t ; i + + { s e l e c t { c a s e r e s : = < - r e s u l t : f m t . P r i n t l n ( " J o b " , i , " " , r e s ) c a s e < - t i m e o u t . C : r e t u r n } } } Run