TRANSILVANIA University of Bra sov [600774]

TRANSILVANIA University of Bra sov
Faculty of Mathematics and Informatics
Master: Internet Technologies
DISSERTATION
Author: Teodora Angelica Dunca
Scienti c coordinator: Prof. univ. dr. Ernest Scheiber
BRAS OV
2012

TRANSILVANIA University of Bra sov
Faculty of Mathematics and Informatics
Master: Internet Technologies
DISTRIBUTED PROGRAMMING
IN GO
Author: Teodora Angelica Dunca
Scienti c coordinator: Prof. univ. dr. Ernest Scheiber
BRAS OV
2012

Content
1 The Go programming language 3
1.1 Installing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Editing programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2.1 First programs in Go . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2.2 Compiling and running . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.3 Notions of syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.3.1 Commentary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.3.2 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.3.3 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.3.4 Control structures . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.3.5 Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2 Concurrent programming in Go 16
2.1 Goroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.2 Channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.3 WaitGroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.4 Select . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.5 Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.6 Once . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3 Network programming with Go 22
3.1 Remote Procedure Call . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.1.1 HTTP RPC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.1.2 TCP RPC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3.1.3 JSON RPC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
3.2 Socket . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
3.2.1 TCP Sockets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Bibliography 78
1

Introduction
"Go is not meant to innovate programming theory.
It's meant to innovate programming practice."
Samuel Tesla
The Go programming language is an expressive, compiled and concurrent program-
ming language developed by Google Inc. . Go language is an open source programming
environment that makes it easy to build simple, productive and ecient project.
The programming language Go is like C++ and Java, but its unusual properties make
programs more ecient, it was designed to reduce the size of code and reduce compilation
time. A transcript of the programs written in Java or C++ in Go, would make the program
acceptable, but if written directly in Go would be a successful and di erent software.
This paper describes how to work with distributed programming in Go and has three
main chapters: The Go programming language ,Concurrent programming in Go and Net-
work programming with Go . In these chapters are presented lexical items and syntax of
the Go programming language (Chapter 1), notions necessary for the concurrent programs
(Chapter 2), and the last chapter shows how to work in network programming with Go
programming language.
The Go programming language is object oriented but does not use objects, such a
program written in Go is a package that import necesary functions from other packages,
packages imported must to be used. In concurrent programming a process is an execution
of a program segment. Advantages of concurrent programming is that multiple programs
can be executed simultaneously and the programs have potentially by interacting with
each other.
The primitives for concurrency present in Go provide an elegant and distinct means
of structuring concurrent software. These concepts are goroutines and channels. Go
channels synchronize and communicate; the sending goroutine waits until the receiving
goroutine is ready to accept the communication.
Go uses several OS threads to do parallel computations and channels. Channels may
be synchronous, or asynchronous. Network Programming is essentially identi ed client-
server programming, is for development of applications that communicate with other
applications in the networking. In the Go programming language remote procedure call
(RPC) is unique, being di erent from the RPC used in other programming languages.
With the explained examples I tried to emphasize the simplicity of editing code and
how easy is to use this language. Also, Go is a new programming language, what is
waiting to be discovered, because it has a lot of new ideas, is innovative, easy and free.
2

Chapter 1
The Go programming language
The Go programming language is an expressive, compiled and concurrent program-
ming language developed by Google Inc. . Go language is an open source programming
environment that makes it easy to build simple, productive and ecient project. The
rst design of Go was done in September 2007 by Robert Griesemer, Rob Pike, and Ken
Thompson, but it was ocially announced in November 2009. The GO programming
language is available for Linux, FreeBSD, Mac OS X and for Windows (since 2011). Go
is in
uenced by programming languages C, Newsqueak, Pascal, Pyton and others.
At rst, Go was not considered to be ready for real production environments. In May
2010, Rob Pike stated publicly that Go is being used "for real stu " at Google. Go is
a very young programming language and is it not revolutionary, it is a good way for
evolution.
J org Walter has written a detailed and positive review of the Go programming lan-
guage. "And as a nal note, I have seen a fair amount of criticism of Go on the internet,
which I cannot ignore, so here it goes: Most of these people didn't actually look at it. Go
is di erent, even though it still looks kinda-C. It isn't. It's not C++, nor Objective C,
and it doesn't try to be! So stop saying 'Who needs Go when we have C++/Objective
C?' already. Check out how Go tries to solve the same problems in a radically di erent
way." [8]
This language was designed to reduce the size of code written in other programming
languages (C, C++, Java), and reduce compilation time. Dynamic languages (Python,
JavaScript) x these problems (no more types, no more compiler) but introduce others like
errors at run time that should be caught statically and no compilation means slow code.
This language was designed because in 10 years it wasn't designed other programming
language. [9]
The Go programming language has been around for almost 3 years and was expe-
rimental when it was released, but because it's easy to learn it will have succed in IT
world.
1.1 Installing
Go is an open source project and is written in C, so to built it you need to install:
3

the GCC2 compiler;
the standard libraries of C;
the Bison generator;
make;
awk;
the text editor ed.
The instalation of the program is in the command line. On the website of the
programming language (http://golang.org/doc/install.html) you nd all versions of the
installation for both platforms OS X and for Linux. For the OS X platform is not
ready but you can use it. For more information go to (http://code.google.com/p/go-
wiki/wiki/WindowsSupport) and (http://code.google.com/p/go/wiki/WindowsPort). For
easier code editing was developed a tool called Gocode. How to do that you nd on
(https://github.com/nsf/gocode). If the program installation is successful, it nishes by
printing output:
ALL TESTS PASSED
Installed Go for linux/amd64 in /home/you/go.
Installed commands in /home/you/go/bin.
*** You need to add /home/you/go/bin to your $PATH. ***
The compiler is 6g.
where the details on the last few lines re
ect the operating system, architecture, and root
directory used during the install. Depending of the computer con guration, will return
compiler version used.
To renew the installed version of GO, software updates will use the following com-
mands:
cd go/src
hg pull
hg update release
./all.bash
For a better use of program commands is recommended to set the PATH system
variable, with the path where is installed GO. Also, you have to set these variables in
your pro le:
GOROOT=$HOME/GO
GOARCH=386
GOOS=LINUX
where, GOROOT represent the path where the distribution has been installed, GOARCH
represent the type of used architecture, and GOOS represent the operating system.
4

1.2 Editing programs
Go programs use the UTF-8 encoding and have the extension .go. Editing of programs
is made by text editor, the most popular may even have support for Go color syntax
highlighting and automatic indentation.
In Go to run a program with the name rst.go will follow the steps:
compiling program: 6g rst.go ;
realization a connection to the compiled le: 6l rst.6 ;
running program: ./6.out
Where the compiler version used is number 6.
1.2.1 First programs in Go
A program in the Go programming language looks like:
Example 1.2.1
1 package main
3 import "fmt"
5 func main ( )f
6 fmt . P r i n t f ( " I j u s t want to say hy ! ! ! nn" )
7g
I just want to say hy!!!
This program is saved name rstand extension .go: ( rst.go ).
The rst statement in a Go source is:
1package packagename
where packagename is the package's default name for imports, all les in a package
must use the same name. In our example the package name is main . Other packages can
be imported to use another functions. This program import the package fmt, because
uses the function Printf . A reason why Go programs are build so quickly is because we
do not need to compile any other packages, rst.go uses standard library packages.
The Go programming language is object oriented but does not use objects, such a
program written in Go is a package that import necesary functions from other packages,
packages imported must to be used.
Is not a problem if in Go the separator ";" is not put at the end of the instructions,
because Go is automatically inserted after each line. But it is sometimes necessary to use
it, for example declaring several variables, etc..
5

It is mandatory to leave one empty line between the package name, import statement
and functions of the program. If this feature is not observed, it will signal to the compiler.
The next program in the Go programming language is about adding two numbers.
This program is saved name addand extension .go: (add.go ). We write the two numbers
and after compilation the result is displayed.
Example 1.2.2
1package main
3import "fmt"
5func main ()f
6 var e r r e r r o r
7 var M, N, sum f l o a t 6 4
9 p r i n t ( " F i r s t number : " )
10 , e r r = fmt . Scanln(& M)
11 i fe r r != n i lf
12 fmt . Println ( " Error : " , e r r )
13g
15 p r i n t ( "Second number : " )
16 , e r r = fmt . Scanln(&N)
17 i fe r r != n i lf
18 fmt . Println ( " Error : " , e r r )
19g
21 sum = M + N;
22 fmt . Println ( "Sum: " , sum)
23g
The third program in the Go programming language makes convert from degrees
Celsius to degrees Fahrenheit and vice versa. This program is saved name conversion and
extension .go: (conversion.go ). We write the two degrees and after compilation the result
is displayed.
Example 1.2.3
1package main
3import "fmt"
5func main ()f
6 var e r r e r r o r
7 var Sens int
9 p r i n t ( " Sens : " )
10 , e r r = fmt . Scanln(&Sens )
11 i fe r r != n i lf
12 fmt . Println ( " Error : " , e r r )
13g
15 var G f l o a t 6 4
16 p r i n t ( "Grade : " )
17 , e r r = fmt . Scanln(&G)
18 i fe r r != n i lf
19 fmt . Println ( " Error : " , e r r )
20g
6

22 i fSens == 0f
23 fmt . Println ( "C2F =" , 1.8 G+32)
24gelsef
25 i fSens == 1f
26 fmt . Println ( "F2C =" ,(G 32)/1.8)
27 gelsef
28 fmt . Println ( " You ' re a s t r a n g e r ! " )
29 g
30 g
31g
This program computes the greatest common divisor of two numbers. This program
is saved name cmmdc and extension .go: (cmmdc.go ). We write the two numbers and
after compilation the result is displayed.
Example 1.2.4
1package main
3import "fmt" ;
5func main ()f
7 var e r r e r r o r
8 var M, N int
10 p r i n t ( "M: " )
11 , e r r = fmt . Scanln(& M)
12 i fe r r != n i lf
13 fmt . Println ( " Error : " , e r r )
14g
16 p r i n t ( "N: " )
17 , e r r = fmt . Scanln(&N)
18 i fe r r != n i lf
19 fmt . Println ( " Error : " , e r r )
20g
22 var C int
23 for R:=1; R!=0;f
24 C = N;
25 R = M % N;
26 M = N;
27 N = R;
28g
30 fmt . Println ( "Cmmdc: " , C)
31g
The next program will display the conversion between the USA, EUR and RON.
Introduce the type of the conversion and the value and after compilation the result is
displayed. This program is saved name Convmoney and extension .go: (Convmoney.go ).
Example 1.2.5
1package main
3import "fmt"
5func main ()f
6 var e r r e r r o r
7

7 var val , do l a r i , euro f l o a t 6 4
8 var from f l o a t 6 4
10 p r i n t ( "From : " )
11 , e r r = fmt . Scanln(&from )
12 i fe r r != n i lf
13 fmt . Println ( " Error : " , e r r )
14g
16 p r i n t ( "Value : " )
17 , e r r = fmt . Scanln(&val )
18 i fe r r != n i lf
19 fmt . Println ( " Error : " , e r r )
20g
22 d o l a r i = 3 . 5 5 7 3 ;
23 euro = 4 . 4 4 4 1 ;
25 i ffrom == 10f
26 fmt . Println ( "RON = >EURO =" , val / euro )
27g
29 i ffrom == 11f
30 fmt . Println ( "RON = >USD =" , val / d o l a r i )
31g
33 i ffrom == 20f
34 fmt . Println ( "EURO = >RON =" , valeuro )
35g
37 i ffrom == 22f
38 fmt . Println ( "USD = >RON =" , vald o l a r i )
39g
41 i ffrom == 30f
42 fmt . Println ( "EURO = >USD =" , valeuro / d o l a r i )
43g
45 i ffrom == 33f
46 fmt . Println ( "USD = >EURO =" , vald o l a r i / euro )
47g
48g
The last program will determine the European zodiac. This program is saved name
zodiac and extension .go: (zodiac.go ).
Example 1.2.6
1package main
3import "fmt"
5func main ()f
6 var e r r e r r o r
7 var month , day int
9 p r i n t ( "Day : " )
10 , e r r = fmt . Scanln(&day )
11 i fe r r != n i lf
12 fmt . Println ( " Error : " , e r r )
13g
15 p r i n t ( "Month : " )
16 , e r r = fmt . Scanln(&month)
17 i fe r r != n i lf
8

18 fmt . Println ( " Error : " , e r r )
19g
21 i f( ( month == 12 && day >= 22 && day <= 31)
22 jj(month == 1 && day >= 1 && day <= 19))f
23 fmt . Println ( " Capricorn " )
24gelsef
25 i f( ( month == 1 && day >= 20 && day <= 31)
26 jj(month == 2 && day >= 1 && day <= 17))f
27 fmt . Println ( " Aquarius " )
28gelsef
29 i f( ( month == 2 && day >= 18 && day <= 29)
30 jj(month == 3 && day >= 1 && day <= 19))f
31 fmt . Println ( " P i s c e s " )
32gelsef
33 i f( ( month == 3 && day >= 20 && day <= 31)
34 jj(month == 4 && day >= 1 && day <= 19))f
35 fmt . Println ( " Aries " )
36gelsef
37 i f( ( month == 4 && day >= 20 && day <= 30)
38 jj(month == 5 && day >= 1 && day <= 20))f
39 fmt . Println ( " Bull " )
40 gelsef
41 i f( ( month == 5 && day >= 21 && day <= 31)
42 jj(month == 6 && day >= 1 && day <= 20))f
43 fmt . Println ( "Gemini" )
44gelsef
45 i f( ( month == 6 && day >= 21 && day <= 30)
46 jj(month == 7 && day >= 1 && day <= 22))f
47 fmt . Println ( "Cancer" )
48gelsef
49 i f( ( month == 7 && day >= 23 && day <= 31)
50 jj(month == 8 && day >= 1 && day <= 22))f
51 fmt . Println ( "Leo" )
52gelsef
53 i f( ( month == 8 && day >= 23 && day <= 31)
54 jj(month == 9 && day >= 1 && day <= 22))f
55 fmt . Println ( " Virgin " )
56gelsef
57 i f( ( month == 9 && day >= 23 && day <= 30)
58 jj(month == 10 && day >= 1 && day <= 22))f
59 fmt . Println ( " Libra " )
60gelsef
61 i f( ( month == 10 && day >= 23 && day <= 31)
62 jj(month == 11 && day >= 1 && day <= 21))f
63 fmt . Println ( " Scorpio " )
64gelsef
65 i f( ( month == 11 && day >= 22 && day <= 30)
66 jj(month == 12 && day >= 1 && day <= 21))f
67 fmt . Println ( " S a g i t t a r i u s " )
68gelsef
69 fmt . Println ( " I l l e g a l date " )
70 g
71 g
72 g
73 g
74 g
75 g
76 g
77 g
78 g
79 g
80g
81g
82g
9

1.2.2 Compiling and running
The Go compiler Gccgo is a compiler that is based on compiler GCC (GNU Compiler
Collection), compiler used and for languages C, C + + and Java.
If our architecture is i386 we have the following program names:
8g- compiles .go programs;
8l- produce binaries;
8c- code generation bug;
8a- is an assembler.
for amd64 these binaries will be named di erently: 6g, 6l, 6c, 6a.
Compiling and running a go program takes three steps:
run '6g rst.go '
– compiles rst.go programs to go object code, rst.6.
run '6l rst.6 '
– it makes a linker which takes go object code and produces binaries, 6.out.
run '6.out '
– by running this le will display the message, "I just want to say hy!!!" .
Another way to compile and run a program is using the command gomake .
1.3 Notions of syntax
The programming language Go is like C++ and Java, but its unusual properties make
programs more ecient. A transcript of the programs written in Java or C++ in Go,
would make the program acceptable, but if written directly in Go would be a successful
and di erent software. It is very important and recommended as well to understand
properties and expressions, so that programs to be easily understood by developers of Go.
1.3.1 Commentary
Comments in Go are the same like in C and C++:
/* */ { block comments like in C style (block comments appear mostly as package
comments);
/* this is
a comment */
=={ line comments like in C++ style (line comments are the norm).
// this is a comment
10

1.3.2 Syntax
The syntax is almost the same like in C or C++, but in Go is more strong. It will
use the varkeyword to declare variables, const keyword to declare constants and the type
keyword for user-de ned types. To observe the di erent ways of declaring the pointers,
the vectors, and how is a variable initialized.
Constants
Constants in Go are created at compile time and can only be numbers, strings or
booleans. In Go constants with value numbers are created by using the iotaenumerator.
1const one = 1
We can group the constants by using round brackets:
1const (
2January = i o t a ; // 0
3February = i o t a ; // 1
4)
Variables
Variables can be initialized just like constants but the initializer can be a general
expression computed at run time.
1var x int
2var y , zint
3var n [ ] int
or group of variables:
1var (
2 xint
3 y , zint
4 n [ ] int
5)
Note that using the separator ";" to separate variables.
Another way to declare and initialize variables is by using the operator ":=", so:
var v = value;
can be written like:
v := value
To initialize global variables you can use one of the two methods provided:
initializing variables when are declared;
use function init() for each variable.
11

De ned types
De ned types are declared using a construction like:
1type Equation s t r u c t f
2 x , y , z float ;
3 name s t r i n g
4g
1.3.3 Operators
In Go are four categories of operators:
binary operators;
unary operators;
operator new;
assignment operators.
Binary operators
In Go can use the following binary operators:
Previous Operators
6 *=% &
5 + -j^
4 == ! = <>
3 <{
2 &&
1jj
Binary^operator is used for operation "or exclusively" ( XOR ).
Unary operators
In Go can use the following unary operators: & ! * + { ^<.
Unary^operator is operator of "complementary" (bitwise negation).
In Go, operators C: "++" and "- -", are not recognized as operators for expressions,
they are considered statements, but their e ect is the same. Operators +=, -=, etc. have
the same functionality as in C.
12

Operator new
In Go the operator new() is used as in C++, is a call to a function. It returns a pointer
to the created object:
var t *Point = new(Point)
z := new(int) // z is of type *int
Don't use methods such as delete orfree to release memory as in C++, in Go have
aGarbage collector that frees unused memory and allocated memory.
Assignment operators
As in other programming languages operator =is used for assignment. In the Go
programming language is allowed multiple assignments, meaning, using a single call of
operator can be assigned multiple variables with di erent values:
u, t, v = f1(), f2(), f3()
m, n = 1, 10/5
where f1(), f2(), f3() are functions.
If the function returns more than one value (which is possible in Go), is used the
operator: :=.
nbytes, eroare := Write(buf)
1.3.4 Control structures
The instructions in GO are similar to those of C or Java, but are improved, so that
initialization can use in the if statement or two counters in a single statement for.
If
In Go a simple if statement looks like this:
1i fa>0f
2 return b
3g
here we don't use anymore ( )and thefgare required. Using GO initialization of
variables can be made since the declaration of instruction condition.
1i fx:=0 f ( ) ; x <20f
2 fmt . P r i n t f ( "%d x i s smaller than 20 nn" )
3g
4elsef
5 fmt . P r i n t f ( "%d x i s not smaller than 20 nn" )
6g
where f()is a function that returns an integer value. To use this feature should be that
after initialization to use the empty statement ";".
13

For
In Go a simple if statement looks like this:
1for i :=0; i <10; i++f. . .g
Where conditions are missing loop enters a in nite cycle:
1for ; ;ffmt . P r i n t f ( " i n f i n i t e c y c l e " ) g
1forffmt . P r i n t f ( " i n f i n i t e c y c l e " ) g
Also, it can use multiple counters declared in the same block, so the sequence is:
1for i :=0; i <M; i++
2f
3 for j :=M; j >0; j f . . .g
4g
a simple form of this is:
1for i , j :=0 , M; i <M, j >0; i , j=i +1, j 1f. . .g
Switch
The statement switch in Go is more general than in C, it has fewer restrictions and
more features, such as:
the expressions need not be constants or integers;
is not necessary to use the instruction break;
if returning the same result can be written on the one case separated by commas;
if you want to through all the branches of instruction, will be added at the end of
instructions at each branch – fallthrough;
the cases are evaluated top to bottom until a match is found, and if the switch has
no expression it switches on true.
1switch count%9f
2 case 3 , 4 , 5 : e r r o r ( ) ;
3 case 2 : x=i ; f a l l t h r o u g h ;
4 case 1 : x=i ; f a l l t h r o u g h ;
5 case 0 :return xi ;
6g
14

1x , y:=a [ i ] , b [ j ] ;
2switchf
3 case x<y :return1;
4 case x==y : return 0 ;
5 case x>b :return 1 ;
6g
or:
1switch x , y := a [ i ] , b [ j ] ; f. . .g
At the same like in C, and in Go the statement swich has branch default :
1switch xf
2 case 0 : fmt . P r i n t f ( "0" )
3 default : fmt . p r i n t f ( " nonzero " )
4g
1.3.5 Errors
The Go libraries can return some sort of error indication to the caller, error values are
used to indicate an abnormal state. Errors have type error, a simple built-in interface:
1type e r r o r interfacef
2 Error ( ) s t r i n g
3g
Close function returns a non-nil error value when it fails to close a le:
1func Close (name s t r i n g ) ( f i l e File , e r r e r r o r )
The fmtpackage formats an error value and know to call the method when asked to
print an error. If the error is unrecoverable and the program can't continue it is use a
built-in function panic , that in e ect creates a run-time error that will stop the program.
When panic is called it immediately stops execution, and the program can die, but
it is possible to use the built-in function recover to regain control of the goroutine and
resume normal execution. Recover is only useful inside deferred functions and is used to
shut down a failing goroutine without killing the other executing goroutines.
15

Chapter 2
Concurrent programming in Go
Concurrent programming is the development of programs that consist of segments that
have the property for parallel execution. Concurrent programming is useful in modeling
or simulating physical systems, even if those systems are not directly controlled by a
computer. For example, the waiting and service times in a bank, supermarket, or other
service organization can be studied by writing a program in which each customer and
each server { bank teller, supermarket checker, airline reservation clerk{is represented by
its own program segment, which interacts with the other segments. [10]
In concurrent programming a process is an execution of a program segment. Advan-
tages of concurrent programming is that multiple programs can be executed simultane-
ously and the programs have potentially by interacting with each other.
The primitives for concurrency present in Go provide an elegant and distinct means
of structuring concurrent software. These concepts are goroutines and channels. Go
channels synchronize and communicate; the sending goroutine waits until the receiving
goroutine is ready to accept the communication.
2.1 Goroutines
In the Go documentation we nd "Goroutines are multiplexed onto multiple OS
threads so if one should block, such as while waiting for I/O, others continue to run". A
goroutine allow executing tasks concurrently at the same address. Goroutines in Go are
similar to normal functions, but you have to attach a goin front of the actual function
call. A program can have more than one goroutine.
" There are a few things to keep in mind about Go's goroutines.
They are not threads in the sense of java's or c++ threads.
– they are more like greenlets.
The go runtime multiplexes the goroutines across the system threads
– the number of system threads is controlled by an environment variable GOMAX-
PROCS and defaults to 1 currently I think. This may change in the future.
16

The way goroutines yield back to their current thread is controlled by several dif-
ferent constructs.
– the select statement can yield control back to the thread.
– sending on a channel can yield control back to the thread.
– doing IO operations can yield control back to the thread.
– runtime.Gosched() explicitly yields control back to the thread.
The behavior you are seeing is caused by because the main function never yields back to
the thread and is instead involved in a busy loop and since there is only one thread the
main loop has no place to run." [11]
A routine ends when its reaches its end or the method runtime.Goexit method() is
called.
2.2 Channels
The most convenient method used for synchronization in Go are channels. Channels
are used to send and receive messages between routines and for synchronization of routines.
When using the communication channels, the comunication is synchronous. If we do not
have the same type of receiver as transmitter, to receive information transmitted, the
communication channel is locked, and vice versa.
Declaring a data type that will use a communication channel is made by:
chan data type
Because channels are reference types, variables used in the communication channels
are initialized with the function make:
var ch := make(chan bool)
to the variable ch is assigned a communication channel for the type bool.
To transmit data over this channel is used the operator <{. When sending a value
place the channel on the left side of the <{ operator, and when we get a value place the
channel on the right. The order how we send and receive messages is very important. If
the channel has no direction , the channel is bi-directional.
sending message on the channel ch is:
ch<– true //true value is transmitted on ch
To assign a value transmitted by channel ch to a variable we use the operator =, and
to initiate a variable with a value transmitted by the channel ch we use the operator :=.
If we don't use the sent value is not necessary to write this, declare as:
<– ch
17

Example 2.2.1
1package main
3import "fmt"
5func g e n e r a t e i n t e g e r s ( ) chan intf
6 ch := make( chan int) ;
7 go func ()f
8 for i := 2 ; ; i++ f
9 ch<i ;
10g
11g( ) ;
12 return ch ;
13g
15func f i l t e r m u l t i p l e s ( in chan int, prime int) chan intf
16 out := make( chan int) ;
17 go func ( )f
18 forf
19 i fi := <in ; i % prime != 0 f
20 out<i ;
21g
22g
23g( ) ;
24 return out ;
25g
27func s i e v e ( ) chan intf
28 out := make( chan int) ;
29 go func ( )f
30 ch := g e n e r a t e i n t e g e r s ( ) ;
31 forf
32 prime := <ch ;
33 out<prime ;
34 ch = f i l t e r m u l t i p l e s ( ch , prime ) ;
35g
36g( ) ;
37 return out ;
38g
40func main ( )f
41 primes := s i e v e ( ) ;
42 forf
43 fmt . Println( <primes ) ;
44g
45g
In this example displays the rst 100 primes.1
2.3 WaitGroup
WaitGroup is nd in the sync package and is used to stop the execution of a group of
routines. WaitGroup is used to block a goroutine has nished. A goroutines calls Addto
set the number of goroutines to wait for and runs and calls Done when nished, as:
func (wg *WaitGroup) Add(delta int)
func (wg *WaitGroup) Done()
1Example taken from: http://ideone.com/5RJ9g
18

func (wg *WaitGroup) Wait()
Example 2.3.1
1var z sync . Mutex
2var wg sync . WaitGroup
3wg . Add(n)
4 for i := 0 ; i <n ; i++f
5 go Read (m, &wg)
6 wg . done ( )
7g()
9g
10wg . Wait ( )
11g
2.4 Select
The select statement is used for channels and is structured like a switch statement.
All case statement are evaluated but runs the communications channel which is available.
If there is no available communication channel will block the execution until one of
the channels will become available, but if there is a default statement will run it. If there
is more communication channels, it will choose randomly one of them.
Example 2.4.1
1package main
3import "fmt"
5func f i b o n a c c i ( c , quit chan int)f
6 x , y := 1 , 1
7 forf
8 s e l e c tf
9 case c<x :
10 x , y = y , x + y
11 case <quit :
12 fmt . Println ( " quit " )
13 return
14 g
15g
16g
18func main ( )f
19 c := make( chan int)
20 quit := make( chan int)
21 go func ( )f
22 for i := 0 ; i <10; i++f
23 fmt . Println( <c )
24 g
25 quit <0
26g()
27 f i b o n a c c i ( c , quit )
28g
19

1
1
2
3
5
8
13
21
34
55
quit
In the previous example we saw how is used the statement select and the statement
case.1
2.5 Locks
Goroutines locks until changes of variables or calls function to be perceived by the
compiler, so that these modi cations are done correctly.
Within the sync package is de ned data type Mutex that has two methods used to
lock and unlock routines:
func (m *Mutex) Lock()
– If m is in a routine which is running, the current routine is blocked when calling
this function.
func (m *Mutex) Unlock()
– Unlock a goroutine. It is important that when you call, m to be blocked, otherwise
it will get a runtime error.
To unlock and lock the variables used to read and write data we use RWMutex type.
RWMutex has de ned methods for blocking the variables, so you can write and read
operations for their unlocking, as:
func (rw *RWMutex) Lock()
– Lock locks rw for writing.
func (rw *RWMutex) RLock()
– RLock locks rw for reading.
func (rw *RWMutex) Unlock()
– Unlock a routine that was blocked for writing. It is important that routine that
unlocks have been locked for writing, otherwise it will get a runtime error.
1http://tour.golang.org/Example taken from: http://tour.golang.org/ – slide 66
20

func (rw *RWMutex) RUnlock()
– Unlock a routine that was blocked forreading. Routine that is unlocked must have
been blocked for reading, otherwise it will get a runtime error.
Example 2.5.1
1var z sync . Mutex
2var i s t r i n g
4func g ( )f
5 i = " 1 , 100"
6 z . Unlock ( )
7g
9func main ( )f
10 z . Lock ( )
11 go g ( )
12 z . Lock ( )
13 p r i n t ( a )
14g
2.6 Once
Within the sync package we nd the statement Once which has implemented the
method:
func (o *Once) Do(f func())
Once is an object that will perform exactly one action. The function Docalls the
function transmitted as a parameter only once. If the function Dois called several times,
the function call is made transmitted as a parameter just once time, even if every other f
function call returns a di erent result.
Example 2.6.1
1var once sync . Once
2for i := 0 ; i <10; i++f
3 go func ( )f
4 // do somethig
5 once . Do( Stop )
6 g()
8g
21

Chapter 3
Network programming with Go
Go uses several OS threads to do parallel computations and channels. Channels may
be synchronous, or asynchronous. Network Programming is essentially identi ed client-
server programming, is for development of applications that communicate with other
applications in the networking.
The client is for initiating the communication and the server is the program which is
waiting to get a message from the client.Both programs, client and the server, are the
distributed system. Is necessary to set up the path for two computers, if you want to can
send messages in a session.
3.1 Remote Procedure Call
The remote procedure call is used to program in the network. Remote Procedure Call
means calling a method of an object from another computer as if it were located on the
local computer. RPC is called remote invocation or remote method invocation. A client
sends a request message to a known remote server to execute a speci ed procedure with
supplied parameters, this is called RPC. A di erence between remote procedure calls and
local calls is that remote calls can fail because of unpredictable network problems.
In the Go programming language RPC is unique, being di erent from the RPC used in
other programming languages. In Go a RPC client sends messages only to a server. The
RPC has restrictions on what functions can be called in the network, it can determine
the value arguments to be sent and reference arguments to get answers and how to signal
errors.
A restriction on arguments is that they must de ne a structure type. An example of
a structure type given in the Go documentation is:
1type Args s t r u c t f
2 A, B int
3g
22

3.1.1 HTTP RPC
Go use HTTP (Hypertext Transfer Protocol) to get messages across the network. The
HTTP mechanism has the advantage that can use the HTTP suport library.
To start an HTTP server you need an RPC handler to the HTTP layer, this one is
HandleHTTP . The client set up an HTTP connection to the RPC server.
Package rpc provides access to the exported methods of an object across a network or
other I/O connection.
The method looks like:
1func (uU) MethodName( argType U1, replyType U2) e r r o rf
2 // missing code
3 return n i l
4g
where U, U1 and U2 can be marshaled by encoding/gob. These requirements apply even
if a di erent codec is used.
The rst argument of the method is provided by the caller. Te second one is the
returned parameters to the caller. If the method returns non-nil then it's value is rep-
resented as a string. The client sees the method's returned value like it was created by
errors.New.
The Dial (DialHTTP ) function realizess both steps for a raw network connection (an
HTTP connection). The Client object has the next two methods, Call and Go, that says
the service and method to call, a pointer is for the arguments, and another one for the
receiving result parameters.
In order to complete, the Call method needs to wait the remote call. The other
method, Go, starts the call asynchronously and nishes by using the Call structure.
The server calls (for HTTP service):
1func main ( )f
3 r e s u l t := new( Result )
4 rpc . Register ( r e s u l t )
5 rpc . HandleHTTP ( )
7 e r r := http . ListenAndServe ( " :1234 " , n i l )
8 i fe r r != n i lf
9 fmt . Println ( e r r . Error ( ) )
10g
11g
Now, using a method "Result.MethodName" the clients can see a service "Result". In
order to do this, a client rst dials the server:
1c l i e n t , e r r := rpc . DialHTTP( " tcp " , serverAddress + " :1234 " )
2i fe r r != n i lf
3 log . Fatal ( " d i a l i n g : " , e r r )
4g
After that a remote call can be made:
23

1argType := Args fM, Ng
2 var replyType int
4 e r r = c l i e n t . Call ( " Result . Add" , argType , &replyType )
5 i fe r r != n i lf
6 log . Fatal ( " r e s u l t e r r o r : " , e r r )
7g
8 fmt . P r i n t f ( " Result : " , replyType )
Client – server applications with HTTP RPC:
Example 3.1.1 The client – server system to calculate the sum of two numbers. The
client program connects to the server. After that the client transmits to the server the two
numbers and gets the results that then displays it.
The server:
1/
2ArithServer
3/
5package main
7import (
8 "fmt"
9 " net / rpc "
10 " net / http "
11)
13type Args s t r u c t f
14 M, N int
15g
17type Result int
19func ( tResult ) Add( args Args , sumint) e r r o rf
20sum = args .M + args .N
21 return n i l
22g
24func main ( )f
26 r e s u l t := new( Result )
27 rpc . Register ( r e s u l t )
28 rpc . HandleHTTP ( )
30 e r r := http . ListenAndServe ( " :1234 " , n i l )
31 i fe r r != n i lf
32 fmt . Println ( e r r . Error ( ) )
33g
34g
The client:
1/
2ArithClient
3/
5package main
7import (
24

8 " net / rpc "
9 "fmt"
10 " log "
11 " os "
12)
14type Args s t r u c t f
15 M, N int
16g
18func main ( )f
19 i flen ( os . Args ) != 2 f
20 fmt . Println ( "Usage : " , os . Args [ 0 ] , " s e r v e r " )
21 os . Exit (1)
22g
24 var e r r e r r o r
25 var M, N int
27 p r i n t ( " F i r s t number : " )
28 , e r r = fmt . Scanln(& M)
30 p r i n t ( "Second number : " )
31 , e r r = fmt . Scanln(&N)
33 serverAddress := os . Args [ 1 ]
35 c l i e n t , e r r := rpc . DialHTTP( " tcp " , serverAddress+" :1234 " )
36 i fe r r != n i lf
37 log . Fatal ( " d i a l i n g : " , e r r )
38g
40 // Synchronous c a l l
41 args := ArgsfM, Ng
42 var sum int
44 e r r = c l i e n t . Call ( " Result . Add" , args , &sum)
45 i fe r r != n i lf
46 log . Fatal ( " r e s u l t e r r o r : " , e r r )
47g
48 fmt . P r i n t f ( " Result : " , sum)
49g
Running the programs
First you must set your path in spath.bat le and run it from the command prompt
as:
c:ngonbin>spath.bat
The code for spath is:
1s e t G O R O O T =c : ngo
2s e t GOOS =windows
3s e t GOARCH=386
4s e t GOBIN =% G O R O O T % nbin
5s e t PATH =% GOBIN%;%PATH %
After that you have to run the server HTTPSumaServer and then open another cmd
(command prompt) window and run the client HTTPSumaClient . The client can run on
any computer in the network. If the client is running from another computer than the
computer where the server program is running, then you have to say the server computer
name.
25

To run the server you have to write in command line:
go run HTTPSumaServer.go
To run the client you have to write in command line:
go run HTTPSumaClient.go localhost
Example 3.1.2 The client – server system for calculating the greatest common divisor
of two integers. The client program connects to the server. After that the client transmits
to the server the two numbers and gets results that then displays it.
The server:
1/
2ArithServer
3/
5package main
7import (
8 "fmt"
9 " net / rpc "
10 " net / http "
11)
13type Args s t r u c t f
14 M, N int
15g
17type Result int
19func ( tResult ) Add( args Args , Cint) e r r o rf
21 for R:=1; R!=0;f
22 C =args .N;
23 R =args .M %args .N;
24 args .M =args .N;
25 args .N = R;
26g
27 return n i l
28g
30func main ( )f
32 r e s u l t := new( Result )
33 rpc . Register ( r e s u l t )
34 rpc . HandleHTTP ( )
36 e r r := http . ListenAndServe ( " :1234 " , n i l )
37 i fe r r != n i lf
38 fmt . Println ( e r r . Error ( ) )
39g
40g
The client:
1/
2ArithClient
3/
26

5package main
7import (
8 " net / rpc "
9 "fmt"
10 " log "
11 " os "
12)
14type Args s t r u c t f
15 M, N int
16g
18func main ( )f
19 i flen ( os . Args ) != 2 f
20 fmt . Println ( "Usage : " , os . Args [ 0 ] , " s e r v e r " )
21 os . Exit (1)
22g
23 var e r r e r r o r
24 var M, N int
26 p r i n t ( "M: " )
27 , e r r = fmt . Scanln(& M)
29 p r i n t ( "N: " )
30 , e r r = fmt . Scanln(&N)
32 serverAddress := os . Args [ 1 ]
34 c l i e n t , e r r := rpc . DialHTTP( " tcp " , serverAddress+" :1234 " )
35 i fe r r != n i lf
36 log . Fatal ( " d i a l i n g : " , e r r )
37g
38 // Synchronous c a l l
39 args := ArgsfM, Ng
40 var C int
42 e r r = c l i e n t . Call ( " Result . Add" , args , & C)
43 i fe r r != n i lf
44 log . Fatal ( " r e s u l t e r r o r : " , e r r )
45g
46 fmt . P r i n t f ( " Result : " , C)
47g
Running the programs
First you must set your path in spath.bat le and run it from the command prompt
as:
c:ngonbin>spath.bat
The code for spath is:
1s e t G O R O O T =c : ngo
2s e t GOOS =windows
3s e t GOARCH=386
4s e t GOBIN =% G O R O O T % nbin
5s e t PATH =% GOBIN%;%PATH %
After that you have to run the server HTTPCmmdcServer and then open another cmd
window and run the client HTTPCmmdcClient . The client can run on any computer in
the network. If the client is running from another computer than the computer where the
server program is running, then you have to say the server computer name.
To run the server you have to write in command line:
27

go run HTTPCmmdcServer.go
To run the client you have to write in command line:
go run HTTPCmmdcClient.go localhost
Example 3.1.3 The client – server system to calculate the conversion from degrees Cel-
sius to degrees Fahrenheit and vice versa. The client program connects to the server. After
that the client transmits to the server the value of degrees and the type of conversion and
gets the results that then displays it.
The server:
1/
2ArithServer
3/
5package main
7import (
8 "fmt"
9 " net / rpc "
10 " net / http "
11)
13type Args s t r u c t f
14 Sens , G f l o a t 6 4
15g
17type Result f l o a t 6 4
19func ( tResult ) Add( args Args , Resf l o a t 6 4 ) e r r o r f
21 i fargs . Sens == 0 f
22 Res = 1.8args .G+32
23gelsef
24 i fargs . Sens == 1 f
25 Res = ( args .G 32)/1.8
26 gelsef
27 fmt . Println ( " You ' re a s t r a n g e r ! " )
28 g
29 g
30 return n i l
31g
33func main ( )f
35 r e s u l t := new( Result )
36 rpc . Register ( r e s u l t )
37 rpc . HandleHTTP ( )
39 e r r := http . ListenAndServe ( " :1234 " , n i l )
40 i fe r r != n i lf
41 fmt . Println ( e r r . Error ( ) )
42g
43g
The client:
1/
2ArithClient
3/
28

5package main
7import (
8 " net / rpc "
9 "fmt"
10 " log "
11 " os "
12)
14type Args s t r u c t f
15 Sens , G f l o a t 6 4
16g
18func main ( )f
19 i flen ( os . Args ) != 2 f
20 fmt . Println ( "Usage : " , os . Args [ 0 ] , " s e r v e r " )
21 os . Exit (1)
22g
23 var e r r e r r o r
24 var Res , G, Sens f l o a t 6 4
26 p r i n t ( " Sens : " )
27 , e r r = fmt . Scanln(&Sens )
29 p r i n t ( "Grade : " )
30 , e r r = fmt . Scanln(&G)
32 serverAddress := os . Args [ 1 ]
34 c l i e n t , e r r := rpc . DialHTTP( " tcp " , serverAddress+" :1234 " )
35 i fe r r != n i lf
36 log . Fatal ( " d i a l i n g : " , e r r )
37g
39 // Synchronous c a l l
40 args := ArgsfSens , Gg
42 e r r = c l i e n t . Call ( " Result . Add" , args , &Res )
43 i fe r r != n i lf
44 log . Fatal ( " r e s u l t e r r o r : " , e r r )
45g
46 fmt . P r i n t f ( " Result : " , Res )
47g
Running the programs
First you must set your path in spath.bat le and run it from the command prompt
as:
c:ngonbin>spath.bat
The code for spath is:
1s e t G O R O O T =c : ngo
2s e t GOOS =windows
3s e t GOARCH=386
4s e t GOBIN =% G O R O O T % nbin
5s e t PATH =% GOBIN%;%PATH %
After that you have to run the server HTTPConvServer and then open another cmd
window and run the client HTTPConvClient . The client can run on any computer in the
network. If the client is running from another computer than the computer where the
server program is running, then you have to say the server computer name.
29

To run the server you have to write in command line:
go run HTTPConvServer.go
To run the client you have to write in command line:
go run HTTPConvClient.go localhost
Example 3.1.4 The client – server system to calculate the converion betwen USD, EURO
and RON. The client program connects to the server. After that the client transmits to
the server the type of conversion and value, takes the results that then displays it.
The server:
1/
2ArithServer
3/
5package main
7import (
8 "fmt"
9 " net / rpc "
10 " net / http "
11)
13type Args s t r u c t f
14 From , Val f l o a t 6 4
15g
17type Result f l o a t 6 4
19func ( tResult ) Add( args Args , Resf l o a t 6 4 ) e r r o r f
21 var d o l a r i , euro f l o a t 6 4
22 d o l a r i = 3 . 5 5 7 3 ;
23 euro = 4 . 4 4 4 1 ;
25 i fargs . From == 10 f
26 Res = args . Val/ euro
27g
29 i fargs . From == 11 f
30 Res = args . Val/ d o l a r i
31g
33 i fargs . From == 20 f
34 Res = args . Val euro
35g
37 i fargs . From == 22 f
38 Res = args . Val d o l a r i
39g
41 i fargs . From == 30 f
42 Res = args . Val euro / d o l a r i
43g
45 i fargs . From == 33 f
46 Res = args . Val d o l a r i / euro
47g
49 return n i l
30

50g
52func main ( )f
54 r e s u l t := new( Result )
55 rpc . Register ( r e s u l t )
56 rpc . HandleHTTP ( )
58 e r r := http . ListenAndServe ( " :1234 " , n i l )
59 i fe r r != n i lf
60 fmt . Println ( e r r . Error ( ) )
61g
62g
The client:
1/
2ArithClient
3/
5package main
7import (
8 " net / rpc "
9 "fmt"
10 " log "
11 " os "
12)
14type Args s t r u c t f
15 From , Val f l o a t 6 4
16g
18func main ( )f
19 i flen ( os . Args ) != 2 f
20 fmt . Println ( "Usage : " , os . Args [ 0 ] , " s e r v e r " )
21 os . Exit (1)
22g
24 var e r r e r r o r
25 var from , val f l o a t 6 4
27 p r i n t ( "From : " )
28 , e r r = fmt . Scanln(&from )
30 p r i n t ( "Value : " )
31 , e r r = fmt . Scanln(&val )
33 serverAddress := os . Args [ 1 ]
35 c l i e n t , e r r := rpc . DialHTTP( " tcp " , serverAddress+" :1234 " )
36 i fe r r != n i lf
37 log . Fatal ( " d i a l i n g : " , e r r )
38g
40 // Synchronous c a l l
41 args := Argsffrom , valg
42 var Res f l o a t 6 4
44 e r r = c l i e n t . Call ( " Result . Add" , args , &Res )
45 i fe r r != n i lf
46 log . Fatal ( " r e s u l t e r r o r : " , e r r )
47g
48 fmt . P r i n t f ( " Result : " , Res )
49g
31

Running the programs
First you must set your path in spath.bat le and run it from the command prompt
as:
c:ngonbin>spath.bat
The code for spath is:
1s e t G O R O O T =c : ngo
2s e t GOOS =windows
3s e t GOARCH=386
4s e t GOBIN =% G O R O O T % nbin
5s e t PATH =% GOBIN%;%PATH %
After that you have to run the server HTTPConvmoneyServer and then open another
cmd (command prompt) window and run the client HTTPConvmoneyClient . The client
can run on any computer in the network. If the client is running from another computer
than the computer where the server program is running, then you have to say the server
computer name.
To run the server you have to write in command line:
go run HTTPConvmoneyServer.go
To run the client you have to write in command line:
go run HTTPConvmoneyClient.go localhost
Example 3.1.5 The client – server system to calculate the european zodiac. The client
program connects to the server. After that the client transmits to the server the month
and the day of born, takes the result and then displays it.
The server:
1/
2ArithServer
3/
5package main
7import (
8 "fmt"
9 " net / rpc "
10 " net / http "
11)
13type Args s t r u c t f
14 Month , Day int
15g
17type Result s t r i n g
19func ( tResult ) Add( args Args , Ress t r i n g ) e r r o r f
20i f( ( args . Month == 12 && args . Day >= 22 && args . Day <= 31)
21 jj( args . Month == 1 && args . Day >= 1 && args . Day <= 19))f
22 fmt . Println ( " Capricorn " )
23gelsef
32

24 i f( ( args . Month == 1 && args . Day >= 20 && args . Day <= 31)
25 jj( args . Month == 2 && args . Day >= 1 && args . Day <= 17))f
26 fmt . Println ( " Aquarius " )
27gelsef
28 i f( ( args . Month == 2 && args . Day >= 18 && args . Day <= 29)
29 jj( args . Month == 3 && args . Day >= 1 && args . Day <= 19))f
30 fmt . Println ( " P i s c e s " )
31gelsef
32 i f( ( args . Month == 3 && args . Day >= 20 && args . Day <= 31)
33 jj( args . Month == 4 && args . Day >= 1 && args . Day <= 19))f
34 fmt . Println ( " Aries " )
35gelsef
36 i f( ( args . Month == 4 && args . Day >= 20 && args . Day <= 30)
37 jj( args . Month == 5 && args . Day >= 1 && args . Day <= 20))f
38 fmt . Println ( " Bull " )
39 gelsef
40 i f( ( args . Month == 5 && args . Day >= 21 && args . Day <= 31)
41 jj( args . Month == 6 && args . Day >= 1 && args . Day <= 20))f
42 fmt . Println ( "Gemini" )
43gelsef
44 i f( ( args . Month == 6 && args . Day >= 21 && args . Day <= 30)
45 jj( args . Month == 7 && args . Day >= 1 && args . Day <= 22))f
46 fmt . Println ( "Cancer" )
47gelsef
48 i f( ( args . Month == 7 && args . Day >= 23 && args . Day <= 31)
49 jj( args . Month == 8 && args . Day >= 1 && args . Day <= 22))f
50 fmt . Println ( "Leo" )
51gelsef
52 i f( ( args . Month == 8 && args . Day >= 23 && args . Day <= 31)
53 jj( args . Month == 9 && args . Day >= 1 && args . Day <= 22))f
54 fmt . Println ( " Virgin " )
55gelsef
56 i f( ( args . Month == 9 && args . Day >= 23 && args . Day <= 30)
57 jj( args . Month == 10 && args . Day >= 1 && args . Day <= 22))f
58 fmt . Println ( " Libra " )
59gelsef
60 i f( ( args . Month == 10 && args . Day >= 23 && args . Day <= 31)
61 jj( args . Month == 11 && args . Day >= 1 && args . Day <= 21))f
62 fmt . Println ( " Scorpio " )
63gelsef
64 i f( ( args . Month == 11 && args . Day >= 22 && args . Day <= 30)
65 jj( args . Month == 12 && args . Day >= 1 && args . Day <= 21))f
66 fmt . Println ( " S a g i t t a r i u s " )
67gelsef
68 fmt . Println ( " I l l e g a l date " )
69 g
70 g
71 g
72 g
73 g
74 g
75 g
76 g
77 g
78 g
79g
80g
81 return n i l
82g
84func main ( )f
86 r e s u l t := new( Result )
87 rpc . Register ( r e s u l t )
88 rpc . HandleHTTP ( )
90 e r r := http . ListenAndServe ( " :1234 " , n i l )
91 i fe r r != n i lf
33

92 fmt . Println ( e r r . Error ( ) )
93g
94g
The client:
1/
2ArithClient
3/
5package main
7import (
8 " net / rpc "
9 "fmt"
10 " log "
11 " os "
12)
14type Args s t r u c t f
15 Month , Day int
16g
18func main ( )f
19 i flen ( os . Args ) != 2 f
20 fmt . Println ( "Usage : " , os . Args [ 0 ] , " s e r v e r " )
21 os . Exit (1)
22g
23 var e r r e r r o r
24 var month , day int
26 p r i n t ( "Day : " )
27 , e r r = fmt . Scanln(&day )
29 p r i n t ( "Month : " )
30 , e r r = fmt . Scanln(&month)
32 serverAddress := os . Args [ 1 ]
34 c l i e n t , e r r := rpc . DialHTTP( " tcp " , serverAddress+" :1234 " )
35 i fe r r != n i lf
36 log . Fatal ( " d i a l i n g : " , e r r )
37g
39 // Synchronous c a l l
40 args := Argsfmonth , dayg
41 var Res s t r i n g
43 e r r = c l i e n t . Call ( " Result . Add" , args , &Res )
44 i fe r r != n i lf
45 log . Fatal ( " r e s u l t e r r o r : " , e r r )
46g
47 fmt . P r i n t f ( " Result : " , Res )
48g
Running the programs
First you must set your path in spath.bat le and run it from the command prompt
as:
c:ngonbin>spath.bat
The code for spath is:
34

1s e t G O R O O T =c : ngo
2s e t GOOS =windows
3s e t GOARCH=386
4s e t GOBIN =% G O R O O T % nbin
5s e t PATH =% GOBIN%;%PATH %
After that you have to run the server HTTPZodiacServer and then open another cmd
(command prompt) window and run the client HTTPZodiacClient . The client can run
on any computer in the network. If the client is running from another computer than the
computer where the server program is running, then you have to say the server computer
name.
To run the server you have to write in command line:
go run HTTPZodiacServer.go
To run the client you have to write in command line:
go run HTTPZodiacClient.go localhost
3.1.2 TCP RPC
Transmission Control Protocol (TCP) is one of the core protocols of the Internet
Protocol Suite. TCP provides a communication service between a request program, the
client, and an Internet Protocol (IP), the server. TCP is used for sending more accurate
data than for sending data to the time, so recorded delays of time.
Package rpcprovides access to the exported methods of an object across a network or
other I/O connection.
The method looks like:
1func (uU) MethodName( argType U1, replyType U2) e r r o rf
2 // missing code
3 return n i l
4g
where U, U1 and U2 can be marshaled by encoding/gob. These requirements apply even
if a di erent codec is used.
The rst argument of the method is provided by the caller. Te second one is the
returned parameters to the caller. If the method returns non-nil then it's value is rep-
resented as a string. The client sees the method's returned value like it was created by
errors.New.
For TCP network address has the form localhost:port . To connect to an RPC server
we use Dial for a speci ed network address. Go invokes the Call function asynchronously.
The done channel will signal when is returned the same Call object, so the call is complete.
The server calls (for TCP service):
35

1func main ( )f
3 r e s u l t := new( Result )
4 rpc . Register ( r e s u l t )
6 tcpAddr , e r r := net . ResolveTCPAddr ( " tcp " , " :1234 " )
7 checkError ( e r r )
9 l i s t e n e r , e r r := net . ListenTCP ( " tcp " , tcpAddr )
10 checkError ( e r r )
12 rpc . Accept ( l i s t e n e r )
14 forf
15 conn , e r r := l i s t e n e r . Accept ( )
16 i fe r r != n i lf
17 continue
18 g
19 rpc . ServeConn ( conn )
20g
21g
23func checkError ( e r r e r r o r ) f
24 i fe r r != n i lf
25 fmt . Println ( " Fatal e r r o r " , e r r . Error ( ) )
26 os . Exit (1)
27g
28g
Now, using a method "Result.MethodName" the clients can see a service "Result". In
order to do this, a client rst dials the server:
1c l i e n t , e r r := rpc . Dial ( " tcp " , s e r v i c e )
2 i fe r r != n i lf
3 log . Fatal ( " d i a l i n g : " , e r r )
4g
After that a remote call can be made:
1argType := Args fM, Ng
2 var replyType int
4 e r r = c l i e n t . Call ( " Result . Add" , argType , &replyType )
5 i fe r r != n i lf
6 log . Fatal ( " r e s u l t e r r o r : " , e r r )
7g
8 fmt . P r i n t f ( " Result : " , replyType )
Client – server applications with TCP RPC:
Example 3.1.6 The client – server system to calculate the sum of two numbers. The
client program connects to the server. After that the client transmits to the server the two
numbers and gets results that then displays it.
The server:
36

1/
2TCPArithServer
3/
5package main
7import (
8 "fmt"
9 " net / rpc "
10 " net "
11 " os "
12)
14type Args s t r u c t f
15 M, N int
16g
18type Result int
20func ( tResult ) Add( args Args , sumint) e r r o rf
21sum = args .M + args .N
22 return n i l
23g
25func main ( )f
27 r e s u l t := new( Result )
28 rpc . Register ( r e s u l t )
30 tcpAddr , e r r := net . ResolveTCPAddr ( " tcp " , " :1234 " )
31 checkError ( e r r )
33 l i s t e n e r , e r r := net . ListenTCP ( " tcp " , tcpAddr )
34 checkError ( e r r )
36 rpc . Accept ( l i s t e n e r )
38 forf
39 conn , e r r := l i s t e n e r . Accept ( )
40 i fe r r != n i lf
41 continue
42 g
43 rpc . ServeConn ( conn )
44g
45g
47func checkError ( e r r e r r o r ) f
48 i fe r r != n i lf
49 fmt . Println ( " Fatal e r r o r " , e r r . Error ( ) )
50 os . Exit (1)
51g
52g
The client:
1/
2TCPArithClient
3/
5package main
7import (
8 " net / rpc "
9 "fmt"
10 " log "
11 " os "
12)
37

14type Args s t r u c t f
15 M, N int
16g
18func main ( )f
19 i flen ( os . Args ) != 2 f
20 fmt . Println ( "Usage : " , os . Args [ 0 ] , " s e r v e r : port " )
21 os . Exit (1)
22g
24 var e r r e r r o r
25 var M, N int
27 p r i n t ( " F i r s t number : " )
28 , e r r = fmt . Scanln(& M)
30 p r i n t ( "Second number : " )
31 , e r r = fmt . Scanln(&N)
33 s e r v i c e := os . Args [ 1 ]
35 c l i e n t , e r r := rpc . Dial ( " tcp " , s e r v i c e )
36 i fe r r != n i lf
37 log . Fatal ( " d i a l i n g : " , e r r )
38g
40 // Synchronous c a l l
41 args := ArgsfM, Ng
42 var sum int
44 e r r = c l i e n t . Call ( " Result . Add" , args , &sum)
45 i fe r r != n i lf
46 log . Fatal ( " r e s u l t e r r o r : " , e r r )
47g
48 fmt . P r i n t f ( " Result : " , sum)
49g
Running the programs
First you must set your path in spath.bat le and run it from the command prompt
as:
c:ngonbin>spath.bat
The code for spath is:
1s e t G O R O O T =c : ngo
2s e t GOOS =windows
3s e t GOARCH=386
4s e t GOBIN =% G O R O O T % nbin
5s e t PATH =% GOBIN%;%PATH %
After that you have to run the server TCPSumaServer and then open another cmd
window and run the client TCPSumaClient . The client can run on any computer in the
network. If the client is running from another computer than the computer where the
server program is running, then you have to say the server computer name.
To run the server you have to write in command line:
go run TCPSumaServer.go
To run the client you have to write in command line:
38

go run TCPSumaClient.go localhost:1234
Example 3.1.7 The client – server system for calculating the greatest common divisor
of two integers. The client program connects to the server. After that the client transmits
to the server the two numbers and gets results that then displays it.
The server:
1/
2TCPArithServer
3/
5package main
7import (
8 "fmt"
9 " net / rpc "
10 " net "
11 " os "
12)
14type Args s t r u c t f
15 M, N int
16g
18type Result int
20func ( tResult ) Add( args Args , Cint) e r r o rf
22 for R:=1; R!=0;f
23 C =args .N;
24 R =args .M %args .N;
25 args .M =args .N;
26 args .N = R;
27g
28 return n i l
29g
31func main ( )f
33 r e s u l t := new( Result )
34 rpc . Register ( r e s u l t )
36 tcpAddr , e r r := net . ResolveTCPAddr ( " tcp " , " :1234 " )
37 checkError ( e r r )
39 l i s t e n e r , e r r := net . ListenTCP ( " tcp " , tcpAddr )
40 checkError ( e r r )
42 rpc . Accept ( l i s t e n e r )
44 forf
45 conn , e r r := l i s t e n e r . Accept ( )
46 i fe r r != n i lf
47 continue
48 g
49 rpc . ServeConn ( conn )
50g
51g
53func checkError ( e r r e r r o r ) f
54 i fe r r != n i lf
55 fmt . Println ( " Fatal e r r o r " , e r r . Error ( ) )
56 os . Exit (1)
57g
58g
39

The client:
1/
2TCPArithClient
3/
5package main
7import (
8 " net / rpc "
9 "fmt"
10 " log "
11 " os "
12)
14type Args s t r u c t f
15 M, N int
16g
18func main ( )f
19 i flen ( os . Args ) != 2 f
20 fmt . Println ( "Usage : " , os . Args [ 0 ] , " s e r v e r : port " )
21 os . Exit (1)
22g
24 var e r r e r r o r
25 var M, N int
27 p r i n t ( "M: " )
28 , e r r = fmt . Scanln(& M)
30 p r i n t ( "N: " )
31 , e r r = fmt . Scanln(&N)
33 s e r v i c e := os . Args [ 1 ]
35 c l i e n t , e r r := rpc . Dial ( " tcp " , s e r v i c e )
36 i fe r r != n i lf
37 log . Fatal ( " d i a l i n g : " , e r r )
38g
40 // Synchronous c a l l
41 args := ArgsfM, Ng
42 var C int
44 e r r = c l i e n t . Call ( " Result . Add" , args , & C)
45 i fe r r != n i lf
46 log . Fatal ( " r e s u l t e r r o r : " , e r r )
47g
48 fmt . P r i n t f ( " Result : " , C)
49g
Running the programs
First you must set your path in spath.bat le and run it from the command prompt
as:
c:ngonbin>spath.bat
The code for spath is:
1s e t G O R O O T =c : ngo
2s e t GOOS =windows
3s e t GOARCH=386
4s e t GOBIN =% G O R O O T % nbin
5s e t PATH =% GOBIN%;%PATH %
40

After that you have to run the server TCPCmmdcServer and then open another cmd
window and run the client TCPCmmdcClient . The client can run on any computer in the
network. If the client is running from another computer than the computer where the
server program is running, then you have to say the server computer name.
To run the server you have to write in command line:
go run TCPCmmdcServer.go
To run the client you have to write in command line:
go run TCPCmmdcClient.go localhost:1234
Example 3.1.8 The client – server system to calculate the conversion from degrees Cel-
sius to degrees Fahrenheit and vice versa. The client program connects to the server. After
that the client transmits to the server the value of degrees and the type of conversion and
gets results that then displays it.
The server:
1/
2TCPArithServer
3/
5package main
7import (
8 "fmt"
9 " net / rpc "
10 " net "
11 " os "
12)
14type Args s t r u c t f
15 Sens , G f l o a t 6 4
16g
18type Result f l o a t 6 4
20func ( tResult ) Add( args Args , Resf l o a t 6 4 ) e r r o r f
22 i fargs . Sens == 0 f
23 Res = 1.8args .G+32
24gelsef
25 i fargs . Sens == 1 f
26 Res = ( args .G 32)/1.8
27 gelsef
28 fmt . Println ( " You ' re a s t r a n g e r ! " )
29 g
30 g
31 return n i l
32g
34func main ( )f
36 r e s u l t := new( Result )
37 rpc . Register ( r e s u l t )
39 tcpAddr , e r r := net . ResolveTCPAddr ( " tcp " , " :1234 " )
40 checkError ( e r r )
41

42 l i s t e n e r , e r r := net . ListenTCP ( " tcp " , tcpAddr )
43 checkError ( e r r )
45 rpc . Accept ( l i s t e n e r )
47 forf
48 conn , e r r := l i s t e n e r . Accept ( )
49 i fe r r != n i lf
50 continue
51 g
52 rpc . ServeConn ( conn )
53g
54g
56func checkError ( e r r e r r o r ) f
57 i fe r r != n i lf
58 fmt . Println ( " Fatal e r r o r " , e r r . Error ( ) )
59 os . Exit (1)
60g
61g
The client:
1/
2TCPArithClient
3/
5package main
7import (
8 " net / rpc "
9 "fmt"
10 " log "
11 " os "
12)
14type Args s t r u c t f
15 Sens , G f l o a t 6 4
16g
18func main ( )f
19 i flen ( os . Args ) != 2 f
20 fmt . Println ( "Usage : " , os . Args [ 0 ] , " s e r v e r : port " )
21 os . Exit (1)
22g
24 var e r r e r r o r
25 var Res , G, Sens f l o a t 6 4
27 p r i n t ( " Sens : " )
28 , e r r = fmt . Scanln(&Sens )
30 p r i n t ( "Grade : " )
31 , e r r = fmt . Scanln(&G)
33 s e r v i c e := os . Args [ 1 ]
35 c l i e n t , e r r := rpc . Dial ( " tcp " , s e r v i c e )
36 i fe r r != n i lf
37 log . Fatal ( " d i a l i n g : " , e r r )
38g
40 // Synchronous c a l l
41 args := ArgsfSens , Gg
43 e r r = c l i e n t . Call ( " Result . Add" , args , &Res )
44 i fe r r != n i lf
42

45 log . Fatal ( " r e s u l t e r r o r : " , e r r )
46g
47 fmt . P r i n t f ( " Result : " , Res )
48g
Running the programs
First you must set your path in spath.bat le and run it from the command prompt
as:
c:ngonbin>spath.bat
The code for spath is:
1s e t G O R O O T =c : ngo
2s e t GOOS =windows
3s e t GOARCH=386
4s e t GOBIN =% G O R O O T % nbin
5s e t PATH =% GOBIN%;%PATH %
After that you have to run the server TCPConvServer and then open another cmd
window and run the client TCPConvClient . The client can run on any computer in the
network. If the client is running from another computer than the computer where the
server program is running, then you have to say the server computer name.
To run the server you have to write in command line:
go run TCPConvServer.go
To run the client you have to write in command line:
go run TCPConvClient.go localhost:1234
Example 3.1.9 The client – server system to calculate the converion betwen USD, EURO
and RON. The client program connects to the server. After that the client transmits to
the server the type of conversion and value, takes the results that then displays it.
The server:
1/
2TCPArithServer
3/
5package main
7import (
8 "fmt"
9 " net / rpc "
10 " net "
11 " os "
12)
14type Args s t r u c t f
15 From , Val f l o a t 6 4
16g
18type Result f l o a t 6 4
43

20func ( tResult ) Convmoney( args Args , Resf l o a t 6 4 ) e r r o r f
22 var d o l a r i , euro f l o a t 6 4
24 d o l a r i = 3 . 5 5 7 3 ;
25 euro = 4 . 4 4 4 1 ;
27 i fargs . From == 10 f
28 Res = args . Val/ euro
29g
31 i fargs . From == 11 f
32 Res = args . Val/ d o l a r i
33g
35 i fargs . From == 20 f
36 Res = args . Val euro
37g
39 i fargs . From == 22 f
40 Res = args . Val d o l a r i
41g
43 i fargs . From == 30 f
44 Res = args . Val euro / d o l a r i
45g
47 i fargs . From == 33 f
48 Res = args . Val d o l a r i / euro
49g
50 return n i l
51g
53func main ( )f
55 r e s u l t := new( Result )
56 rpc . Register ( r e s u l t )
58 tcpAddr , e r r := net . ResolveTCPAddr ( " tcp " , " :1234 " )
59 checkError ( e r r )
61 l i s t e n e r , e r r := net . ListenTCP ( " tcp " , tcpAddr )
62 checkError ( e r r )
64 rpc . Accept ( l i s t e n e r )
66 forf
67 conn , e r r := l i s t e n e r . Accept ( )
68 i fe r r != n i lf
69 continue
70 g
71 rpc . ServeConn ( conn )
72g
73g
75func checkError ( e r r e r r o r ) f
76 i fe r r != n i lf
77 fmt . Println ( " Fatal e r r o r " , e r r . Error ( ) )
78 os . Exit (1)
79g
80g
The client:
1/
2TCPArithClient
3/
44

5package main
7import (
8 " net / rpc "
9 "fmt"
10 " log "
11 " os "
12)
14type Args s t r u c t f
15 From , Val f l o a t 6 4
16g
18func main ( )f
19 i flen ( os . Args ) != 2 f
20 fmt . Println ( "Usage : " , os . Args [ 0 ] , " s e r v e r : port " )
21 os . Exit (1)
22g
24 var e r r e r r o r
25 var Res , from , val f l o a t 6 4
27 p r i n t ( "From : " )
28 , e r r = fmt . Scanln(&from )
31 p r i n t ( "Value : " )
32 , e r r = fmt . Scanln(&val )
34 s e r v i c e := os . Args [ 1 ]
36 c l i e n t , e r r := rpc . Dial ( " tcp " , s e r v i c e )
37 i fe r r != n i lf
38 log . Fatal ( " d i a l i n g : " , e r r )
39g
41 // Synchronous c a l l
42 args := Argsffrom , valg
44 e r r = c l i e n t . Call ( " Result . Convmoney" , args , &Res )
45 i fe r r != n i lf
46 log . Fatal ( " r e s u l t e r r o r : " , e r r )
47g
48 fmt . P r i n t f ( " Result : " , Res )
49g
Running the programs
First you must set your path in spath.bat le and run it from the command prompt
as:
c:ngonbin>spath.bat
The code for spath is:
1s e t G O R O O T =c : ngo
2s e t GOOS =windows
3s e t GOARCH=386
4s e t GOBIN =% G O R O O T % nbin
5s e t PATH =% GOBIN%;%PATH %
After that you have to run the server TCPConvmoneyServer and then open another
cmd (command prompt) window and run the client TCPConvmoneyClient . The client
45

can run on any computer in the network. If the client is running from another computer
than the computer where the server program is running, then you have to say the server
computer name.
To run the server you have to write in command line:
go run TCPConvmoneyServer.go
To run the client you have to write in command line:
go run TCPConvmoneyClient.go localhost:1234
Example 3.1.10 The client – server system to calculate the european zodiac. The client
program connects to the server. After that the client transmits to the server the month
and the day of born, takes the result and then displays it.
The server:
1/
2TCPArithServer
3/
5package main
7import (
8 "fmt"
9 " net / rpc "
10 " net "
11 " os "
12)
14type Args s t r u c t f
15 Month , Day int
16g
18type Result s t r i n g
20func ( tResult ) Zodiac ( args Args , Ress t r i n g ) e r r o r f
21i f( ( args . Month == 12 && args . Day >= 22 && args . Day <= 31)
22 jj( args . Month == 1 && args . Day >= 1 && args . Day <= 19))f
23 fmt . Println ( " Capricorn " )
24gelsef
25 i f( ( args . Month == 1 && args . Day >= 20 && args . Day <= 31)
26 jj( args . Month == 2 && args . Day >= 1 && args . Day <= 17))f
27 fmt . Println ( " Aquarius " )
28gelsef
29 i f( ( args . Month == 2 && args . Day >= 18 && args . Day <= 29)
30 jj( args . Month == 3 && args . Day >= 1 && args . Day <= 19))f
31 fmt . Println ( " P i s c e s " )
32gelsef
33 i f( ( args . Month == 3 && args . Day >= 20 && args . Day <= 31)
34 jj( args . Month == 4 && args . Day >= 1 && args . Day <= 19))f
35 fmt . Println ( " Aries " )
36gelsef
37 i f( ( args . Month == 4 && args . Day >= 20 && args . Day <= 30)
38 jj( args . Month == 5 && args . Day >= 1 && args . Day <= 20))f
39 fmt . Println ( " Bull " )
40 gelsef
41 i f( ( args . Month == 5 && args . Day >= 21 && args . Day <= 31)
42 jj( args . Month == 6 && args . Day >= 1 && args . Day <= 20))f
43 fmt . Println ( "Gemini" )
44gelsef
46

45 i f( ( args . Month == 6 && args . Day >= 21 && args . Day <= 30)
46 jj( args . Month == 7 && args . Day >= 1 && args . Day <= 22))f
47 fmt . Println ( "Cancer" )
48gelsef
49 i f( ( args . Month == 7 && args . Day >= 23 && args . Day <= 31)
50 jj( args . Month == 8 && args . Day >= 1 && args . Day <= 22))f
51 fmt . Println ( "Leo" )
52gelsef
53 i f( ( args . Month == 8 && args . Day >= 23 && args . Day <= 31)
54 jj( args . Month == 9 && args . Day >= 1 && args . Day <= 22))f
55 fmt . Println ( " Virgin " )
56gelsef
57 i f( ( args . Month == 9 && args . Day >= 23 && args . Day <= 30)
58 jj( args . Month == 10 && args . Day >= 1 && args . Day <= 22))f
59 fmt . Println ( " Libra " )
60gelsef
61 i f( ( args . Month == 10 && args . Day >= 23 && args . Day <= 31)
62 jj( args . Month == 11 && args . Day >= 1 && args . Day <= 21))f
63 fmt . Println ( " Scorpio " )
64gelsef
65 i f( ( args . Month == 11 && args . Day >= 22 && args . Day <= 30)
66 jj( args . Month == 12 && args . Day >= 1 && args . Day <= 21))f
67 fmt . Println ( " S a g i t t a r i u s " )
68gelsef
69 fmt . Println ( " I l l e g a l date " )
70 g
71 g
72 g
73 g
74 g
75 g
76 g
77 g
78 g
79 g
80g
81g
82 return n i l
83g
85func main ( )f
87 r e s u l t := new( Result )
88 rpc . Register ( r e s u l t )
90 tcpAddr , e r r := net . ResolveTCPAddr ( " tcp " , " :1234 " )
91 checkError ( e r r )
93 l i s t e n e r , e r r := net . ListenTCP ( " tcp " , tcpAddr )
94 checkError ( e r r )
96 rpc . Accept ( l i s t e n e r )
98 forf
99 conn , e r r := l i s t e n e r . Accept ( )
100 i fe r r != n i lf
101 continue
102 g
103 rpc . ServeConn ( conn )
104g
105g
107func checkError ( e r r e r r o r ) f
108 i fe r r != n i lf
109 fmt . Println ( " Fatal e r r o r " , e r r . Error ( ) )
110 os . Exit (1)
111g
112g
47

The client:
1/
2TCPArithClient
3/
5package main
7import (
8 " net / rpc "
9 "fmt"
10 " log "
11 " os "
12)
14type Args s t r u c t f
15 Month , Day int
16g
18func main ( )f
19 i flen ( os . Args ) != 2 f
20 fmt . Println ( "Usage : " , os . Args [ 0 ] , " s e r v e r : port " )
21 os . Exit (1)
22g
24 var e r r e r r o r
25 var month , day int
26 var Res s t r i n g
28 p r i n t ( "Month : " )
29 , e r r = fmt . Scanln(&month)
31 p r i n t ( "Day : " )
32 , e r r = fmt . Scanln(&day )
34 s e r v i c e := os . Args [ 1 ]
36 c l i e n t , e r r := rpc . Dial ( " tcp " , s e r v i c e )
37 i fe r r != n i lf
38 log . Fatal ( " d i a l i n g : " , e r r )
39g
41 // Synchronous c a l l
42 args := Argsfmonth , dayg
44 e r r = c l i e n t . Call ( " Result . Zodiac " , args , &Res )
45 i fe r r != n i lf
46 log . Fatal ( " r e s u l t e r r o r : " , e r r )
47g
48 fmt . P r i n t f ( " Result : " , Res )
49g
Running the programs
First you must set your path in spath.bat le and run it from the command prompt
as:
c:ngonbin>spath.bat
The code for spath is:
1s e t G O R O O T =c : ngo
2s e t GOOS =windows
3s e t GOARCH=386
4s e t GOBIN =% G O R O O T % nbin
5s e t PATH =% GOBIN%;%PATH %
48

After that you have to run the server TCPZodiacServer and then open another cmd
(command prompt) window and run the client TCPZodiacClient . The client can run on
any computer in the network. If the client is running from another computer than the
computer where the server program is running, then you have to say the server computer
name.
To run the server you have to write in command line:
go run TCPZodiacServer.go
To run the client you have to write in command line:
go run TCPZodiacClient.go localhost:1234
3.1.3 JSON RPC
JSON (JavaScript Object Notation) is a simple data exchange format. The objects and
the lists of JSON resemble those of JavaScript. It is used both for JavaScript programs
running in the browser and by others programs. More information can be found on the
ocial website json.org.
Package rpc provides access to the exported methods of an object across a network or
other I/O connection.
The method looks like:
1func (uU) MethodName( argType U1, replyType U2) e r r o rf
2 // missing code
3 return n i l
4g
where U, U1 and U2 can be marshaled by encoding/gob. These requirements apply even
if a di erent codec is used.
The rst argument of the method is provided by the caller. Te second one is the
returned parameters to the caller. If the method returns non-nil then it's value is rep-
resented as a string. The client sees the method's returned value like it was created by
errors.New.
We use Dial to connect a JSON-RPC server to the speci ed network address. The
JSON-RPC server is run on a single connection by ServeConn .ServeConn is invoked in
a go statement.
The server calls:
1func main ( )f
3 r e s u l t := new( Result )
4 rpc . Register ( r e s u l t )
6 tcpAddr , e r r := net . ResolveTCPAddr ( " tcp " , " :1234 " )
7 checkError ( e r r )
9 l i s t e n e r , e r r := net . ListenTCP ( " tcp " , tcpAddr )
10 checkError ( e r r )
49

12 forf
13 conn , e r r := l i s t e n e r . Accept ( )
14 i fe r r != n i lf
15 continue
16 g
17 jsonrpc . ServeConn ( conn )
18g
19g
21func checkError ( e r r e r r o r ) f
22 i fe r r != n i lf
23 fmt . Println ( " Fatal e r r o r " , e r r . Error ( ) )
24 os . Exit (1)
25g
Now, using a method "Result.MethodName" the clients can see a service "Result". In
order to do this, a client rst dials the server:
1c l i e n t , e r r := jsonrpc . Dial ( " tcp " , s e r v i c e )
2 i fe r r != n i lf
3 log . Fatal ( " d i a l i n g : " , e r r )
4g
After that a remote call can be made:
1argType := Args fM, Ng
2 var replyType int
4 e r r = c l i e n t . Call ( " Result . Add" , argType , &replyType )
5 i fe r r != n i lf
6 log . Fatal ( " r e s u l t e r r o r : " , e r r )
7g
8 fmt . P r i n t f ( " Result : " , replyType )
Client – server applications with JSON RPC:
Example 3.1.11 The client – server system to calculate the sum of two numbers. The
client program connects to the server. After that the client transmits to the server the two
numbers and gets the results that then displays it.
The server:
1/
2JSONArithServer
3/
5package main
7import (
8 "fmt"
9 " net / rpc "
10 " net / rpc / jsonrpc "
11 " os "
12 " net "
13)
15type Args s t r u c t f
16 M, N int
50

17g
19type Result int
21func ( tResult ) Add( args Args , sumint) e r r o rf
22sum = args .M + args .N
23 return n i l
24g
26func main ( )f
28 r e s u l t := new( Result )
29 rpc . Register ( r e s u l t )
31 tcpAddr , e r r := net . ResolveTCPAddr ( " tcp " , " :1234 " )
32 checkError ( e r r )
34 l i s t e n e r , e r r := net . ListenTCP ( " tcp " , tcpAddr )
35 checkError ( e r r )
37 forf
38 conn , e r r := l i s t e n e r . Accept ( )
39 i fe r r != n i lf
40 continue
41 g
42 jsonrpc . ServeConn ( conn )
43g
44g
46func checkError ( e r r e r r o r ) f
47 i fe r r != n i lf
48 fmt . Println ( " Fatal e r r o r " , e r r . Error ( ) )
49 os . Exit (1)
50g
51g
The client:
1/
2JSONArithCLient
3/
5package main
7import (
8 " net / rpc / jsonrpc "
9 "fmt"
10 " log "
11 " os "
12)
14type Args s t r u c t f
15 M, N int
16g
18func main ( )f
19 i flen ( os . Args ) != 2 f
20 fmt . Println ( "Usage : " , os . Args [ 0 ] , " s e r v e r : port " )
21 log . Fatal (1)
22g
24 var e r r e r r o r
25 var M, N int
27 p r i n t ( " F i r s t number : " )
28 , e r r = fmt . Scanln(& M)
51

30 p r i n t ( "Second number : " )
31 , e r r = fmt . Scanln(&N)
33 s e r v i c e := os . Args [ 1 ]
35 c l i e n t , e r r := jsonrpc . Dial ( " tcp " , s e r v i c e )
36 i fe r r != n i lf
37 log . Fatal ( " d i a l i n g : " , e r r )
38g
40 // Synchronous c a l l
41 args := ArgsfM, Ng
42 var sum int
44 e r r = c l i e n t . Call ( " Result . Add" , args , &sum)
45 i fe r r != n i lf
46 log . Fatal ( " r e s u l t e r r o r : " , e r r )
47g
48 fmt . P r i n t f ( " Result : " , sum)
49g
Running the programs
First you must set your path in spath.bat le and run it from the command prompt
as:
c:ngonbin>spath.bat
The code for spath is:
1s e t G O R O O T =c : ngo
2s e t GOOS =windows
3s e t GOARCH=386
4s e t GOBIN =% G O R O O T % nbin
5s e t PATH =% GOBIN%;%PATH %
After that you have to run the server JSONSumaServer and then open another cmd
window and run the client JSONSumaClient . The client can run on any computer in the
network. If the client is running from another computer than the computer where the
server program is running, then you have to say the server computer name.
To run the server you have to write in command line:
go run JSONSumaServer.go
To run the client you have to write in command line:
go run JSONSumaClient.go localhost:1234
Example 3.1.12 The client – server system for calculating the greatest common divisor
of two integers. The client program connects to the server. After that the client transmits
to the server the two numbers and gets the results that then displays it.
The server:
52

1/
2JSONArithServer
3/
5package main
7import (
8 "fmt"
9 " net / rpc "
10 " net / rpc / jsonrpc "
11 " os "
12 " net "
13)
15type Args s t r u c t f
16 M, N int
17g
19type Result int
21func ( tResult ) Add( args Args , Cint) e r r o rf
23 for R:=1; R!=0;f
24 C =args .N;
25 R =args .M %args .N;
26 args .M =args .N;
27 args .N = R;
28g
29 return n i l
30g
32func main ( )f
34 r e s u l t := new( Result )
35 rpc . Register ( r e s u l t )
37 tcpAddr , e r r := net . ResolveTCPAddr ( " tcp " , " :1234 " )
38 checkError ( e r r )
40 l i s t e n e r , e r r := net . ListenTCP ( " tcp " , tcpAddr )
41 checkError ( e r r )
43 forf
44 conn , e r r := l i s t e n e r . Accept ( )
45 i fe r r != n i lf
46 continue
47 g
48 jsonrpc . ServeConn ( conn )
49g
50g
52func checkError ( e r r e r r o r ) f
53 i fe r r != n i lf
54 fmt . Println ( " Fatal e r r o r " , e r r . Error ( ) )
55 os . Exit (1)
56g
57g
The client:
1/
2JSONArithCLient
3/
5package main
7import (
53

8 " net / rpc / jsonrpc "
9 "fmt"
10 " log "
11 " os "
12)
14type Args s t r u c t f
15 M, N int
16g
18func main ( )f
19 i flen ( os . Args ) != 2 f
20 fmt . Println ( "Usage : " , os . Args [ 0 ] , " s e r v e r : port " )
21 log . Fatal (1)
22g
24 var e r r e r r o r
25 var M, N int
27 p r i n t ( "M: " )
28 , e r r = fmt . Scanln(& M)
30 p r i n t ( "N: " )
31 , e r r = fmt . Scanln(&N)
33 s e r v i c e := os . Args [ 1 ]
35 c l i e n t , e r r := jsonrpc . Dial ( " tcp " , s e r v i c e )
36 i fe r r != n i lf
37 log . Fatal ( " d i a l i n g : " , e r r )
38g
40 // Synchronous c a l l
41 args := ArgsfM, Ng
42 var C int
44 e r r = c l i e n t . Call ( " Result . Add" , args , & C)
45 i fe r r != n i lf
46 log . Fatal ( " r e s u l t e r r o r : " , e r r )
47g
48 fmt . P r i n t f ( " Result : " , C)
49g
Running the programs
First you must set your path in spath.bat le and run it from the command prompt
as:
c:ngonbin>spath.bat
The code for spath is:
1s e t G O R O O T =c : ngo
2s e t GOOS =windows
3s e t GOARCH=386
4s e t GOBIN =% G O R O O T % nbin
5s e t PATH =% GOBIN%;%PATH %
After that you have to run the server JSONCmmdcServer and then open another cmd
window and run the client JSONCmmdcClient . The client can run on any computer in
the network. If the client is running from another computer than the computer where the
server program is running, then you have to say the server computer name.
To run the server you have to write in command line:
54

go run JSONCmmdcServer.go
To run the client you have to write in command line:
go run JSONCmmdcClient.go localhost:1234
Example 3.1.13 The client – server system to calculate the conversion from degrees
Celsius to degrees Fahrenheit and vice versa. The client program connects to the server.
After that the client transmits to the server the value of degrees and the type of conversion
and gets the results that then displays it.
The server:
1/
2JSONArithServer
3/
5package main
7import (
8 "fmt"
9 " net / rpc "
10 " net / rpc / jsonrpc "
11 " os "
12 " net "
13)
15type Args s t r u c t f
16 Sens , G f l o a t 6 4
17g
19type Result f l o a t 6 4
21func ( tResult ) Add( args Args , Resf l o a t 6 4 ) e r r o r f
23 i fargs . Sens == 0 f
24 Res = 1.8args .G+32
25gelsef
26 i fargs . Sens == 1 f
27 Res = ( args .G 32)/1.8
28 gelsef
29 fmt . Println ( " You ' re a s t r a n g e r ! " )
30 g
31 g
32 return n i l
33g
35func main ( )f
37 r e s u l t := new( Result )
38 rpc . Register ( r e s u l t )
40 tcpAddr , e r r := net . ResolveTCPAddr ( " tcp " , " :1234 " )
41 checkError ( e r r )
43 l i s t e n e r , e r r := net . ListenTCP ( " tcp " , tcpAddr )
44 checkError ( e r r )
46 forf
47 conn , e r r := l i s t e n e r . Accept ( )
48 i fe r r != n i lf
49 continue
50 g
55

51 jsonrpc . ServeConn ( conn )
52g
53g
55func checkError ( e r r e r r o r ) f
56 i fe r r != n i lf
57 fmt . Println ( " Fatal e r r o r " , e r r . Error ( ) )
58 os . Exit (1)
59g
60g
The client:
1/
2JSONArithCLient
3/
5package main
7import (
8 " net / rpc / jsonrpc "
9 "fmt"
10 " log "
11 " os "
12)
14type Args s t r u c t f
15 Sens , G f l o a t 6 4
16g
18func main ( )f
19 i flen ( os . Args ) != 2 f
20 fmt . Println ( "Usage : " , os . Args [ 0 ] , " s e r v e r : port " )
21 log . Fatal (1)
22g
24 var e r r e r r o r
25 var Res , G, Sens f l o a t 6 4
27 p r i n t ( " Sens : " )
28 , e r r = fmt . Scanln(&Sens )
30 p r i n t ( "Grade : " )
31 , e r r = fmt . Scanln(&G)
33 s e r v i c e := os . Args [ 1 ]
35 c l i e n t , e r r := jsonrpc . Dial ( " tcp " , s e r v i c e )
36 i fe r r != n i lf
37 log . Fatal ( " d i a l i n g : " , e r r )
38g
39 // Synchronous c a l l
40 args := ArgsfSens , Gg
42 e r r = c l i e n t . Call ( " Result . Add" , args , &Res )
43 i fe r r != n i lf
44 log . Fatal ( " r e s u l t e r r o r : " , e r r )
45g
46 fmt . P r i n t f ( " Result : " , Res )
47g
Running the programs
First you must set your path in spath.bat le and run it from the command prompt
as:
56

c:ngonbin>spath.bat
The code for spath is:
1s e t G O R O O T =c : ngo
2s e t GOOS =windows
3s e t GOARCH=386
4s e t GOBIN =% G O R O O T % nbin
5s e t PATH =% GOBIN%;%PATH %
After that you have to run the server JSONConvServer and then open another cmd
window and run the client JSONConvClient . The client can run on any computer in the
network. If the client is running from another computer than the computer where the
server program is running, then you have to say the server computer name.
To run the server you have to write in command line:
go run JSONConvServer.go
To run the client you have to write in command line:
go run JSONConvClient.go localhost:1234
Example 3.1.14 The client – server system to calculate the converion betwen USD,
EURO and RON. The client program connects to the server. After that the client trans-
mits to the server the type of conversion and value, takes the results that then displays
it.
The server:
1/
2JSONArithServer
3/
5package main
7import (
8 "fmt"
9 " net / rpc "
10 " net / rpc / jsonrpc "
11 " os "
12 " net "
13)
15type Args s t r u c t f
16 From , Val f l o a t 6 4
17g
19type Result f l o a t 6 4
21func ( tResult ) Convmoney( args Args , Resf l o a t 6 4 ) e r r o r f
23 var d o l a r i , euro f l o a t 6 4
25 d o l a r i = 3 . 5 5 7 3 ;
26 euro = 4 . 4 4 4 1 ;
28 i fargs . From == 10 f
29 Res = args . Val/ euro
57

30g
32 i fargs . From == 11 f
33 Res = args . Val/ d o l a r i
34g
36 i fargs . From == 20 f
37 Res = args . Val euro
38g
40 i fargs . From == 22 f
41 Res = args . Val d o l a r i
42g
44 i fargs . From == 30 f
45 Res = args . Val euro / d o l a r i
46g
48 i fargs . From == 33 f
49 Res = args . Val d o l a r i / euro
50g
52 return n i l
53g
55func main ( )f
57 r e s u l t := new( Result )
58 rpc . Register ( r e s u l t )
60 tcpAddr , e r r := net . ResolveTCPAddr ( " tcp " , " :1234 " )
61 checkError ( e r r )
63 l i s t e n e r , e r r := net . ListenTCP ( " tcp " , tcpAddr )
64 checkError ( e r r )
66 forf
67 conn , e r r := l i s t e n e r . Accept ( )
68 i fe r r != n i lf
69 continue
70 g
71 jsonrpc . ServeConn ( conn )
72g
73g
75func checkError ( e r r e r r o r ) f
76 i fe r r != n i lf
77 fmt . Println ( " Fatal e r r o r " , e r r . Error ( ) )
78 os . Exit (1)
79g
80g
The client:
1/
2JSONArithCLient
3/
5package main
7import (
8 " net / rpc / jsonrpc "
9 "fmt"
10 " log "
11 " os "
12)
58

14type Args s t r u c t f
15 From , Val f l o a t 6 4
16g
18func main ( )f
19 i flen ( os . Args ) != 2 f
20 fmt . Println ( "Usage : " , os . Args [ 0 ] , " s e r v e r : port " )
21 log . Fatal (1)
22g
24 var e r r e r r o r
25 var Res , val , from f l o a t 6 4
27 p r i n t ( "From : " )
28 , e r r = fmt . Scanln(&from )
30 p r i n t ( "Value : " )
31 , e r r = fmt . Scanln(&val )
33 s e r v i c e := os . Args [ 1 ]
35 c l i e n t , e r r := jsonrpc . Dial ( " tcp " , s e r v i c e )
36 i fe r r != n i lf
37 log . Fatal ( " d i a l i n g : " , e r r )
38g
39 // Synchronous c a l l
40 args := Argsffrom , valg
42 e r r = c l i e n t . Call ( " Result . Convmoney" , args , &Res )
43 i fe r r != n i lf
44 log . Fatal ( " r e s u l t e r r o r : " , e r r )
45g
46 fmt . P r i n t f ( " Result : " , Res )
47g
Running the programs
First you must set your path in spath.bat le and run it from the command prompt
as:
c:ngonbin>spath.bat
The code for spath is:
1s e t G O R O O T =c : ngo
2s e t GOOS =windows
3s e t GOARCH=386
4s e t GOBIN =% G O R O O T % nbin
5s e t PATH =% GOBIN%;%PATH %
After that you have to run the server JSONConvmoneyServer and then open another
cmd (command prompt) window and run the client JSONConvmoneyClient . The client
can run on any computer in the network. If the client is running from another computer
than the computer where the server program is running, then you have to say the server
computer name.
To run the server you have to write in command line:
go run JSONConvmoneyServer.go
To run the client you have to write in command line:
59

go run JSONConvmoneyClient.go localhost:1234
Example 3.1.15 The client – server system to calculate the european zodiac. The client
program connects to the server. After that the client transmits to the server the month
and the day of born, takes the result and then displays it.
The server:
1/
2JSONArithServer
3/
5package main
7import (
8 "fmt"
9 " net / rpc "
10 " net / rpc / jsonrpc "
11 " os "
12 " net "
13)
15type Args s t r u c t f
16 Month , Day int
17g
19type Result s t r i n g
21func ( tResult ) Add( args Args , Ress t r i n g ) e r r o r f
22i f( ( args . Month == 12 && args . Day >= 22 && args . Day <= 31)
23 jj( args . Month == 1 && args . Day >= 1 && args . Day <= 19))f
24 fmt . Println ( " Capricorn " )
25gelsef
26 i f( ( args . Month == 1 && args . Day >= 20 && args . Day <= 31)
27 jj( args . Month == 2 && args . Day >= 1 && args . Day <= 17))f
28 fmt . Println ( " Aquarius " )
29gelsef
30 i f( ( args . Month == 2 && args . Day >= 18 && args . Day <= 29)
31 jj( args . Month == 3 && args . Day >= 1 && args . Day <= 19))f
32 fmt . Println ( " P i s c e s " )
33gelsef
34 i f( ( args . Month == 3 && args . Day >= 20 && args . Day <= 31)
35 jj( args . Month == 4 && args . Day >= 1 && args . Day <= 19))f
36 fmt . Println ( " Aries " )
37gelsef
38 i f( ( args . Month == 4 && args . Day >= 20 && args . Day <= 30)
39 jj( args . Month == 5 && args . Day >= 1 && args . Day <= 20))f
40 fmt . Println ( " Bull " )
41 gelsef
42 i f( ( args . Month == 5 && args . Day >= 21 && args . Day <= 31)
43 jj( args . Month == 6 && args . Day >= 1 && args . Day <= 20))f
44 fmt . Println ( "Gemini" )
45gelsef
46 i f( ( args . Month == 6 && args . Day >= 21 && args . Day <= 30)
47 jj( args . Month == 7 && args . Day >= 1 && args . Day <= 22))f
48 fmt . Println ( "Cancer" )
49gelsef
50 i f( ( args . Month == 7 && args . Day >= 23 && args . Day <= 31)
51 jj( args . Month == 8 && args . Day >= 1 && args . Day <= 22))f
52 fmt . Println ( "Leo" )
53gelsef
54 i f( ( args . Month == 8 && args . Day >= 23 && args . Day <= 31)
55 jj( args . Month == 9 && args . Day >= 1 && args . Day <= 22))f
56 fmt . Println ( " Virgin " )
57gelsef
60

58 i f( ( args . Month == 9 && args . Day >= 23 && args . Day <= 30)
59 jj( args . Month == 10 && args . Day >= 1 && args . Day <= 22))f
60 fmt . Println ( " Libra " )
61gelsef
62 i f( ( args . Month == 10 && args . Day >= 23 && args . Day <= 31)
63 jj( args . Month == 11 && args . Day >= 1 && args . Day <= 21))f
64 fmt . Println ( " Scorpio " )
65gelsef
66 i f( ( args . Month == 11 && args . Day >= 22 && args . Day <= 30)
67 jj( args . Month == 12 && args . Day >= 1 && args . Day <= 21))f
68 fmt . Println ( " S a g i t t a r i u s " )
69gelsef
70 fmt . Println ( " I l l e g a l date " )
71 g
72 g
73 g
74 g
75 g
76 g
77 g
78 g
79 g
80 g
81g
82g
83 return n i l
84g
86func main ( )f
88 r e s u l t := new( Result )
89 rpc . Register ( r e s u l t )
91 tcpAddr , e r r := net . ResolveTCPAddr ( " tcp " , " :1234 " )
92 checkError ( e r r )
94 l i s t e n e r , e r r := net . ListenTCP ( " tcp " , tcpAddr )
95 checkError ( e r r )
97 forf
98 conn , e r r := l i s t e n e r . Accept ( )
99 i fe r r != n i lf
100 continue
101 g
102 jsonrpc . ServeConn ( conn )
103g
104g
106func checkError ( e r r e r r o r ) f
107 i fe r r != n i lf
108 fmt . Println ( " Fatal e r r o r " , e r r . Error ( ) )
109 os . Exit (1)
110g
111g
The client:
1/
2JSONArithCLient
3/
5package main
7import (
8 " net / rpc / jsonrpc "
9 "fmt"
10 " log "
61

11 " os "
12)
14type Args s t r u c t f
15 Month , Day int
16g
18func main ( )f
19 i flen ( os . Args ) != 2 f
20 fmt . Println ( "Usage : " , os . Args [ 0 ] , " s e r v e r : port " )
21 log . Fatal (1)
22g
24 var e r r e r r o r
25 var month , day int
27 p r i n t ( "Month : " )
28 , e r r = fmt . Scanln(&month)
30 p r i n t ( "Day : " )
31 , e r r = fmt . Scanln(&day )
33 s e r v i c e := os . Args [ 1 ]
35 c l i e n t , e r r := jsonrpc . Dial ( " tcp " , s e r v i c e )
36 i fe r r != n i lf
37 log . Fatal ( " d i a l i n g : " , e r r )
38g
40 // Synchronous c a l l
41 args := Argsfmonth , dayg
42 var Res s t r i n g
44 e r r = c l i e n t . Call ( " Result . Add" , args , &Res )
45 i fe r r != n i lf
46 log . Fatal ( " r e s u l t e r r o r : " , e r r )
47g
48 fmt . P r i n t f ( " Result : " , Res )
49g
Running the programs
First you must set your path in spath.bat le and run it from the command prompt
as:
c:ngonbin>spath.bat
The code for spath is:
1s e t G O R O O T =c : ngo
2s e t GOOS =windows
3s e t GOARCH=386
4s e t GOBIN =% G O R O O T % nbin
5s e t PATH =% GOBIN%;%PATH %
After that you have to run the server JSONZodiacServer and then open another cmd
(command prompt) window and run the client JSONZodiacClient . The client can run on
any computer in the network. If the client is running from another computer than the
computer where the server program is running, then you have to say the server computer
name.
To run the server you have to write in command line:
62

go run JSONZodiacServer.go
To run the client you have to write in command line:
go run JSONZodiacClient.go localhost:1234
3.2 Socket
A socket is the end of a bidirectional communication paths between two programs
running on your network. A socket is connected to a TCP port which can identify the
application to which data are transmitted. In order to communicate both, client and
server have to read and write data to the socket connected to the connection between
them.
The client knows the hostname of the server running and port to which connected. To
establish the connection, the client attempts a rendez-vous with server on server machine
and to the server port. If all goes well, the server accepts the connection. After acceptance,
the server creates a new socket to the client connected to another port so that requests to
initial socket could continue while connected client requests are satis ed. From the client,
after accepting the socket connection is created and is used for communication with the
server.
3.2.1 TCP Sockets
For communicate using TCP the client aplication and server aplication establish a
secure conection. Each program is tied to the connection by a socket. TCP is when two
applications communicate with each other to establish a connection through which to
exchange data. Using TCP, communication ensures that data sent from one end to reach
the other end to keep the order in which they were sent. This type of communication
is like a phone conversation. TCP provides a secure channel of communication between
applications.
Package rpc provides access to the exported methods of an object across a network or
other I/O connection.
The method looks like:
1func (uU) MethodName( argType U1, replyType U2) e r r o rf
2 // missing code
3 return n i l
4g
where U, U1 and U2 can be marshaled by encoding/gob. These requirements apply even
if a di erent codec is used.
The rst argument of the method is provided by the caller. Te second one is the
returned parameters to the caller. If the method returns non-nil then it's value is rep-
resented as a string. The client sees the method's returned value like it was created by
errors.New.
The Listen function creates servers:
63

1func main ( )f
2 fmt . Println ( " s t a r t " ) ;
3 s e r v i c e := " :8080 "
4 ln , e r r := net . Listen ( " tcp " , s e r v i c e )
5 i fe r r != n i lf
6 // handle error
7g
8 forf
9 conn , e r r := ln . Accept ( )
10 i fe r r != n i lf
11 continue
12g
13 go handleConnection ( conn )
14g
15g
The Dial function connects to a server:
1conn , e r r := net . Dial ( " tcp " , s e r v i c e )
2 i fe r r != n i lf
3 log . Fatal ( " Connection e r r o r " , e r r )
4g
Conn implements the Reader and Writer interfaces, which makes it easy to use: you
can give it to a Decoder orEncoder . When you listen on a socket, you should pass the open
socket to a goroutine that will handle it. What is great is that a socket is bidirectional.
1func handleConnection ( conn net . Conn) f
3 dec := gob . NewDecoder ( conn )
4 num := &Pfg
5 r e s := & Qfg
7 dec . Decode (num)
8 fmt . P r i n t f ( " Received from c l i e n t : %+v nn" , num ) ;
10 r e s . Add = num.M + num.N
12 encoder := gob . NewEncoder ( conn )
13 encoder . Encode ( r e s )
14g
Client – server applications with TCP Socket:
Example 3.2.1 The client – server system to calculate the sum of two numbers. The
client program connects to the server. After that the client transmits to the server the two
numbers and gets the results that then displays it.
The server:
1/
2SocketArithServer
3/
5package main
64

7import (
8 "fmt"
9 " net "
10 " encoding /gob"
11)
13type P s t r u c t f
14 M, N int64
15g
17type Q s t r u c t f
18 Add int64
19g
21func handleConnection ( conn net . Conn) f
23 dec := gob . NewDecoder ( conn )
24 num := &Pfg
25 r e s := & Qfg
27 dec . Decode (num)
28 fmt . P r i n t f ( " Received from c l i e n t : %+v nn" , num ) ;
30 r e s . Add = num.M + num.N
32 encoder := gob . NewEncoder ( conn )
33 encoder . Encode ( r e s )
34g
36func main ( )f
37 fmt . Println ( " s t a r t " ) ;
38 s e r v i c e := " :8080 "
39 ln , e r r := net . Listen ( " tcp " , s e r v i c e )
40 i fe r r != n i lf
41 // handle error
42g
43 forf
44 conn , e r r := ln . Accept ( )
45 i fe r r != n i lf
46 continue
47g
48 go handleConnection ( conn )
49g
50g
The client:
1/
2SocketArithClient
3/
5package main
7import (
8 "fmt"
9 " log "
10 " net "
11 " os "
12 " encoding /gob"
13)
15type P s t r u c t f
16 M, N int64
17g
19type Q s t r u c t f
20 Add int64
65

21g
23func main ( )f
24 fmt . Println ( " s t a r t c l i e n t " ) ;
25 i flen ( os . Args ) != 2 f
26 fmt . Fprintf ( os . Stderr , "Usage : %s host : port " , os . Args [ 0 ] )
27 os . Exit (1)
28g
29 s e r v i c e := os . Args [ 1 ]
31 conn , e r r := net . Dial ( " tcp " , s e r v i c e )
32 i fe r r != n i lf
33 log . Fatal ( " Connection e r r o r " , e r r )
34g
36 encoder := gob . NewEncoder ( conn )
37 var M, N int64
39 p r i n t ( " F i r s t number : " )
40 , e r r = fmt . Scanln(& M)
42 p r i n t ( "Second number : " )
43 , e r r = fmt . Scanln(&N)
45 var p P
46 p .M = M
47 p .N = N
49 num := &Pfp .M, p .Ng
50 encoder . Encode (num)
52 dec := gob . NewDecoder ( conn )
53 resFromServer := & Q fg
54 dec . Decode ( resFromServer )
56 fmt . P r i n t f ( " Received from s e r v e r : % +v nn" , resFromServer ) ;
57 fmt . Println ( "done" ) ;
58g
Running the programs
First you must set your path in spath.bat le and run it from the command prompt
as:
c:ngonbin>spath.bat
The code for spath is:
1s e t G O R O O T =c : ngo
2s e t GOOS =windows
3s e t GOARCH=386
4s e t GOBIN =% G O R O O T % nbin
5s e t PATH =% GOBIN%;%PATH %
After that you have to run the server SocketSumaServer and then open another cmd
window and run the client SocketSumaClient . The client can run on any computer in the
network. If the client is running from another computer than the computer where the
server program is running, then you have to say the server computer name.
To run the server you have to write in command line:
go run SocketSumaServer.go
66

To run the client you have to write in command line:
go run SocketSumaClient.go localhost:8080
Example 3.2.2 The client – server system for calculating the greatest common divisor
of two integers. The client program connects to the server. After that the client transmits
to the server the two numbers and gets results that then displays it.
The server:
1/
2SocketArithServer
3/
5package main
7import (
8 "fmt"
9 " net "
10 " encoding /gob"
11)
13type P s t r u c t f
14 M, N int
15g
17type Q s t r u c t f
18 Cmmdc int
19g
21func handleConnection ( conn net . Conn) f
23 dec := gob . NewDecoder ( conn )
24 num := &Pfg
25 r e s := & Qfg
27 dec . Decode (num)
28 fmt . P r i n t f ( " Received from c l i e n t : %+v nn" , num ) ;
30 for R:=1; R!=0;f
31 r e s .Cmmdc =num.N;
32 R =num.M % num.N;
33 num.M =num.N;
34 num.N = R;
35g
36 encoder := gob . NewEncoder ( conn )
37 encoder . Encode ( r e s )
38g
40func main ( )f
41 fmt . Println ( " s t a r t " ) ;
42 s e r v i c e := " :8080 "
43 ln , e r r := net . Listen ( " tcp " , s e r v i c e )
44 i fe r r != n i lf
45 // handle error
46g
47 forf
48 conn , e r r := ln . Accept ( )
49 i fe r r != n i lf
50 continue
51g
52 go handleConnection ( conn )
53g
54g
67

The client:
1/
2SocketArithClient
3/
5package main
7import (
8 "fmt"
9 " log "
10 " os "
11 " net "
12 " encoding /gob"
13)
15type P s t r u c t f
16 M, N int
17g
19type Q s t r u c t f
20 Cmmdc int
21g
23func main ( )f
24 fmt . Println ( " s t a r t c l i e n t " ) ;
25 i flen ( os . Args ) != 2 f
26 fmt . Fprintf ( os . Stderr , "Usage : %s host : port " , os . Args [ 0 ] )
27 os . Exit (1)
28g
29 s e r v i c e := os . Args [ 1 ]
31 conn , e r r := net . Dial ( " tcp " , s e r v i c e )
32 i fe r r != n i lf
33 log . Fatal ( " Connection e r r o r " , e r r )
34g
36 encoder := gob . NewEncoder ( conn )
37 var M, N int
39 p r i n t ( " F i r s t number : " )
40 , e r r = fmt . Scanln(& M)
42 p r i n t ( "Second number : " )
43 , e r r = fmt . Scanln(&N)
45 var p P
46 p .M = M
47 p .N = N
49 num := &Pfp .M, p .Ng
50 encoder . Encode (num)
52 dec := gob . NewDecoder ( conn )
53 resFromServer := & Q fg
54 dec . Decode ( resFromServer )
56 fmt . P r i n t f ( " Received from s e r v e r : % +v nn" , resFromServer ) ;
57 fmt . Println ( "done" ) ;
58g
Running the programs
First you must set your path in spath.bat le and run it from the command prompt
as:
c:ngonbin>spath.bat
68

The code for spath is:
1s e t G O R O O T =c : ngo
2s e t GOOS =windows
3s e t GOARCH=386
4s e t GOBIN =% G O R O O T % nbin
5s e t PATH =% GOBIN%;%PATH %
After that you have to run the server SocketCmmdcServer and then open another cmd
window and run the client SocketCmmdcClient . The client can run on any computer in
the network. If the client is running from another computer than the computer where the
server program is running, then you have to say the server computer name.
To run the server you have to write in command line:
go run SocketCmmdcServer.go
To run the client you have to write in command line:
go run SocketCmmdcClient.go localhost:8080
Example 3.2.3 The client – server system to calculate the conversion from degrees Cel-
sius to degrees Fahrenheit and vice versa. The client program connects to the server. After
that the client transmits to the server the value of degrees and the type of conversion and
gets the results that then displays it.
The server:
1/
2SocketArithServer
3/
5package main
7import (
8 "fmt"
9 " net "
10 " encoding /gob"
11)
13type P s t r u c t f
14 Sens , G f l o a t 6 4
15g
17type Q s t r u c t f
18 Conv f l o a t 6 4
19g
21func handleConnection ( conn net . Conn) f
23 dec := gob . NewDecoder ( conn )
24 dat := &Pfg
25 r e s := & Qfg
27 dec . Decode ( dat )
28 fmt . P r i n t f ( " Received from c l i e n t : %+v nn" , dat ) ;
29 i fdat . Sens == 0 f
30 r e s . Conv = 1.8 dat .G+32
31 gelsef
32 i fdat . Sens == 1 f
69

33 r e s . Conv = ( dat .G 32)/1.8
34 gelsef
35 fmt . Println ( " You ' re a s t r a n g e r ! " )
36 g
37 g
39 encoder := gob . NewEncoder ( conn )
40 encoder . Encode ( r e s )
41g
43func main ( )f
44 fmt . Println ( " s t a r t " ) ;
45 s e r v i c e := " :8080 "
46 ln , e r r := net . Listen ( " tcp " , s e r v i c e )
47 i fe r r != n i lf
48 // handle error
49g
50 forf
51 conn , e r r := ln . Accept ( )
52 i fe r r != n i lf
53 continue
54g
55 go handleConnection ( conn )
56g
57g
The client:
1/
2SocketArithClient
3/
5package main
7import (
8 "fmt"
9 " log "
10 " net "
11 " os "
12 " encoding /gob"
13)
15type P s t r u c t f
16 Sens , G f l o a t 6 4
17g
19type Q s t r u c t f
20 Conv f l o a t 6 4
21g
23func main ( )f
24 fmt . Println ( " s t a r t c l i e n t " ) ;
25 i flen ( os . Args ) != 2 f
26 fmt . Fprintf ( os . Stderr , "Usage : %s host : port " , os . Args [ 0 ] )
27 os . Exit (1)
28g
29 s e r v i c e := os . Args [ 1 ]
31 conn , e r r := net . Dial ( " tcp " , s e r v i c e )
32 i fe r r != n i lf
33 log . Fatal ( " Connection e r r o r " , e r r )
34g
36 encoder := gob . NewEncoder ( conn )
37 var G, Sens f l o a t 6 4
39 p r i n t ( " Sens : " )
70

40 , e r r = fmt . Scanln(&Sens )
42 p r i n t ( "Grade : " )
43 , e r r = fmt . Scanln(&G)
45 var p P
46 p . Sens = Sens
47 p .G = G
49 dat := &Pfp . Sens , p .Gg
50 encoder . Encode ( dat )
52 dec := gob . NewDecoder ( conn )
53 resFromServer := & Q fg
54 dec . Decode ( resFromServer )
56 fmt . P r i n t f ( " Received from s e r v e r : % +v nn" , resFromServer ) ;
57 fmt . Println ( "done" ) ;
58g
Running the programs
First you must set your path in spath.bat le and run it from the command prompt
as:
c:ngonbin>spath.bat
The code for spath is:
1s e t G O R O O T =c : ngo
2s e t GOOS =windows
3s e t GOARCH=386
4s e t GOBIN =% G O R O O T % nbin
5s e t PATH =% GOBIN%;%PATH %
After that you have to run the server SocketConvServer and then open another cmd
window and run the client SocketConvClient . The client can run on any computer in the
network. If the client is running from another computer than the computer where the
server program is running, then you have to say the server computer name.
To run the server you have to write in command line:
go run SocketConvServer.go
To run the client you have to write in command line:
go run SocketConvClient.go localhost:8080
Example 3.2.4 The client – server system to calculate the converion betwen USD, EURO
and RON. The client program connects to the server. After that the client transmits to
the server the type of conversion and value, takes the results that then displays it.
The server:
71

1/
2SocketArithServer
3/
5package main
7import (
8 "fmt"
9 " net "
10 " encoding /gob"
11)
13type P s t r u c t f
14 From , Val f l o a t 6 4
15g
17type Q s t r u c t f
18 Conv f l o a t 6 4
19g
21func handleConnection ( conn net . Conn) f
23 dec := gob . NewDecoder ( conn )
24 dat := &Pfg
25 r e s := & Qfg
27 var d o l a r i , euro f l o a t 6 4
29 d o l a r i = 3 . 5 5 7 3 ;
30 euro = 4 . 4 4 4 1 ;
32 dec . Decode ( dat )
33 fmt . P r i n t f ( " Received from c l i e n t : %+v nn" , dat ) ;
35 i fdat . From == 10 f
36 r e s . Conv = dat . Val/ euro
37g
39 i fdat . From == 11 f
40 r e s . Conv = dat . Val/ d o l a r i
41g
43 i fdat . From == 20 f
44 r e s . Conv = dat . Val euro
45g
47 i fdat . From == 22 f
48 r e s . Conv = dat . Val d o l a r i
49g
51 i fdat . From == 30 f
52 r e s . Conv = dat . Val euro / d o l a r i
53g
55 i fdat . From == 33 f
56 r e s . Conv = dat . Val d o l a r i / euro
57g
59 encoder := gob . NewEncoder ( conn )
60 encoder . Encode ( r e s )
61g
63func main ( )f
64 fmt . Println ( " s t a r t " ) ;
65 s e r v i c e := " :8080 "
66 ln , e r r := net . Listen ( " tcp " , s e r v i c e )
67 i fe r r != n i lf
72

68 // handle error
69g
70 forf
71 conn , e r r := ln . Accept ( )
72 i fe r r != n i lf
73 continue
74g
75 go handleConnection ( conn )
76g
77g
The client:
1/
2SocketArithClient
3/
5package main
7import (
8 "fmt"
9 " log "
10 " net "
11 " os "
12 " encoding /gob"
13)
15type P s t r u c t f
16 From , Val f l o a t 6 4
17g
19type Q s t r u c t f
20 Conv f l o a t 6 4
21g
23func main ( )f
24 fmt . Println ( " s t a r t c l i e n t " ) ;
25 i flen ( os . Args ) != 2 f
26 fmt . Fprintf ( os . Stderr , "Usage : %s host : port " , os . Args [ 0 ] )
27 os . Exit (1)
28g
29 s e r v i c e := os . Args [ 1 ]
31 conn , e r r := net . Dial ( " tcp " , s e r v i c e )
32 i fe r r != n i lf
33 log . Fatal ( " Connection e r r o r " , e r r )
34g
36 encoder := gob . NewEncoder ( conn )
37 var From , Val f l o a t 6 4
39 p r i n t ( "From : " )
40 , e r r = fmt . Scanln(&From)
42 p r i n t ( "Value : " )
43 , e r r = fmt . Scanln(&Val )
45 var p P
46 p . From = From
47 p . Val = Val
49 dat := &Pfp . From , p . Val g
50 encoder . Encode ( dat )
52 dec := gob . NewDecoder ( conn )
53 resFromServer := & Q fg
54 dec . Decode ( resFromServer )
73

56 fmt . P r i n t f ( " Received from s e r v e r : % +v nn" , resFromServer ) ;
57 fmt . Println ( "done" ) ;
58g
Running the programs
First you must set your path in spath.bat le and run it from the command prompt
as:
c:ngonbin>spath.bat
The code for spath is:
1s e t G O R O O T =c : ngo
2s e t GOOS =windows
3s e t GOARCH=386
4s e t GOBIN =% G O R O O T % nbin
5s e t PATH =% GOBIN%;%PATH %
After that you have to run the server SocketConvmoneyServer and then open another
cmd (command prompt) window and run the client SocketConvmoneyClient . The client
can run on any computer in the network. If the client is running from another computer
than the computer where the server program is running, then you have to say the server
computer name.
To run the server you have to write in command line:
go run SocketConvmoneyServer.go
To run the client you have to write in command line:
go run SocketConvmoneyClient.go localhost:8080
Example 3.2.5 The client – server system to calculate the european zodiac. The client
program connects to the server. After that the client transmits to the server the month
and the day of born, takes the result and then displays it.
The server:
1/
2SocketArithServer
3/
5package main
7import (
8 "fmt"
9 " net "
10 " encoding /gob"
11)
13type P s t r u c t f
14 Month , Day int
15g
17type Q s t r u c t f
74

18 Zodic s t r i n g
19g
21func handleConnection ( conn net . Conn) f
23 dec := gob . NewDecoder ( conn )
24 dat := &Pfg
25 r e s := & Qfg
27 dec . Decode ( dat )
28 fmt . P r i n t f ( " Received from c l i e n t : %+v nn" , dat ) ;
30i f( ( dat . Month == 12 && dat . Day >= 22 && dat . Day <= 31)
31 jj( dat . Month == 1 && dat . Day >= 1 && dat . Day <= 19))f
32 fmt . Println ( " Capricorn " )
33gelsef
34 i f( ( dat . Month == 1 && dat . Day >= 20 && dat . Day <= 31)
35 jj( dat . Month == 2 && dat . Day >= 1 && dat . Day <= 17))f
36 fmt . Println ( " Aquarius " )
37gelsef
38 i f( ( dat . Month == 2 && dat . Day >= 18 && dat . Day <= 29)
39 jj( dat . Month == 3 && dat . Day >= 1 && dat . Day <= 19))f
40 fmt . Println ( " P i s c e s " )
41gelsef
42 i f( ( dat . Month == 3 && dat . Day >= 20 && dat . Day <= 31)
43 jj( dat . Month == 4 && dat . Day >= 1 && dat . Day <= 19))f
44 fmt . Println ( " Aries " )
45gelsef
46 i f( ( dat . Month == 4 && dat . Day >= 20 && dat . Day <= 30)
47 jj( dat . Month == 5 && dat . Day >= 1 && dat . Day <= 20))f
48 fmt . Println ( " Bull " )
49 gelsef
50 i f( ( dat . Month == 5 && dat . Day >= 21 && dat . Day <= 31)
51 jj( dat . Month == 6 && dat . Day >= 1 && dat . Day <= 20))f
52 fmt . Println ( "Gemini" )
53gelsef
54 i f( ( dat . Month == 6 && dat . Day >= 21 && dat . Day <= 30)
55 jj( dat . Month == 7 && dat . Day >= 1 && dat . Day <= 22))f
56 fmt . Println ( "Cancer" )
57gelsef
58 i f( ( dat . Month == 7 && dat . Day >= 23 && dat . Day <= 31)
59 jj( dat . Month == 8 && dat . Day >= 1 && dat . Day <= 22))f
60 fmt . Println ( "Leo" )
61gelsef
62 i f( ( dat . Month == 8 && dat . Day >= 23 && dat . Day <= 31)
63 jj( dat . Month == 9 && dat . Day >= 1 && dat . Day <= 22))f
64 fmt . Println ( " Virgin " )
65gelsef
66 i f( ( dat . Month == 9 && dat . Day >= 23 && dat . Day <= 30)
67 jj( dat . Month == 10 && dat . Day >= 1 && dat . Day <= 22))f
68 fmt . Println ( " Libra " )
69gelsef
70 i f( ( dat . Month == 10 && dat . Day >= 23 && dat . Day <= 31)
71 jj( dat . Month == 11 && dat . Day >= 1 && dat . Day <= 21))f
72 fmt . Println ( " Scorpio " )
73gelsef
74 i f( ( dat . Month == 11 && dat . Day >= 22 && dat . Day <= 30)
75 jj( dat . Month == 12 && dat . Day >= 1 && dat . Day <= 21))f
76 fmt . Println ( " S a g i t t a r i u s " )
77gelsef
78 fmt . Println ( " I l l e g a l date " )
79 g
80 g
81 g
82 g
83 g
84 g
85 g
75

86 g
87 g
88 g
89g
90g
91encoder := gob . NewEncoder ( conn )
92 encoder . Encode ( r e s )
93g
95func main ( )f
96 fmt . Println ( " s t a r t " ) ;
97 s e r v i c e := " :8080 "
98 ln , e r r := net . Listen ( " tcp " , s e r v i c e )
99 i fe r r != n i lf
100 // handle error
101g
102 forf
103 conn , e r r := ln . Accept ( )
104 i fe r r != n i lf
105 continue
106g
107 go handleConnection ( conn )
108g
109g
The client:
1/
2SocketArithClient
3/
5package main
7import (
8 "fmt"
9 " log "
10 " net "
11 " os "
12 " encoding /gob"
13)
15type P s t r u c t f
16 Month , Day int
17g
19type Q s t r u c t f
20 Zodic s t r i n g
21g
23func main ( )f
24 fmt . Println ( " s t a r t c l i e n t " ) ;
25 i flen ( os . Args ) != 2 f
26 fmt . Fprintf ( os . Stderr , "Usage : %s host : port " , os . Args [ 0 ] )
27 os . Exit (1)
28g
29 s e r v i c e := os . Args [ 1 ]
31 conn , e r r := net . Dial ( " tcp " , s e r v i c e )
32 i fe r r != n i lf
33 log . Fatal ( " Connection e r r o r " , e r r )
34g
36 encoder := gob . NewEncoder ( conn )
37 var Month , Day int
39 p r i n t ( "Month : " )
40 , e r r = fmt . Scanln(&Month)
76

42 p r i n t ( "Day : " )
43 , e r r = fmt . Scanln(&Day)
45 var p P
46 p . Month = Month
47 p . Day = Day
49 dat := &Pfp . Month , p . Day g
50 encoder . Encode ( dat )
52 dec := gob . NewDecoder ( conn )
53 resFromServer := & Q fg
54 dec . Decode ( resFromServer )
56 fmt . P r i n t f ( " Received from s e r v e r : % +v nn" , resFromServer ) ;
57 fmt . Println ( "done" ) ;
58g
Running the programs
First you must set your path in spath.bat le and run it from the command prompt
as:
c:ngonbin>spath.bat
The code for spath is:
1s e t G O R O O T =c : ngo
2s e t GOOS =windows
3s e t GOARCH=386
4s e t GOBIN =% G O R O O T % nbin
5s e t PATH =% GOBIN%;%PATH %
After that you have to run the server SocketZodiacServer and then open another cmd
(command prompt) window and run the client SocketZodiacClient . The client can run on
any computer in the network. If the client is running from another computer than the
computer where the server program is running, then you have to say the server computer
name.
To run the server you have to write in command line:
go run SocketZodiacServer.go
To run the client you have to write in command line:
go run SocketZodiacClient.go localhost:8080
77

Bibliography
[1] Newmarch Jan, Network programming with Go, 27 April 2012.
http :==jan:newmarch:name=golang=
[2] Scheiber Ernest, Programare distribuit a ^ n Java, Bra sov.
http :==cs:unitbv:ro=site=pagpers=scheiber=html=Resources=DISTR 1:pd f
[3] Raghu Prabhakar, Rohit Kumar, Concurrent Programming in Go, 03 June 2011.
http :==www:cs:ucla:edu= rohitkk=go manual:pd f
[4] The Go Authors. A Tour of Go, 2012.
http :==tour:golang:org=
[5] The Go Authors. Package sync, 2012.
http :==golang:org=pkg=sync
[6] The Go Authors. The go programming language speci cation, 2012.
http :==golang:org=ref=spec
[7] The Go Authors. E ective go, 2012.
http :==golang:org=doc=effective go:html
[8]http :==lwn:net=Articles= 446598 =
[9]http :==golang:org=doc=talks=go talk20100121 :html
(slide 6 and 7)
[10]http :==www:dei:isep:ipp:pt= npinho=ada 95=chap 15:htm
[11]http :==stackoverflow:com=questions= 10095751 =understandinggoroutines
(answer)
[12]http :==en:wikipedia:org=wiki=Remote procedure call
[13]http :==en:wikipedia:org=wiki=Network socket
78

Similar Posts