good evening again this is the first time
I'm seeing this okay I'm holding it up
this is a gopher Singapore edition so
gopher Merlion and the title of my talk
if I speak it quickly
oops stay
nvm it doesn't want to
title of my talk if you speak it quickly
is Go for you? That's a pun there.
All of us have limited mental capacity
limited mind budget
budget some of us can keep more things
in our mind some of us can keep less
things in our mind but you know in all
cases it's limited so what do you mean
by mine budget if you know how to drive
a car it's instinctive
you still know you still need to know
where you're going so that consumes
energy and mine budget so the point I'm
trying to ask is what are your current
skills what you currently do today and
what you want to do tomorrow so for
example if I currently write stuff for
the web
typically web front-ends I know
JavaScript quite well by definition but
I want to write back-end stuff around
the right back-end servers I want to
write API service your look you know
because you can write back-end stuff in
JavaScript it's called no GS you still
need to know about the rules of writing
API servers you still need to learn
about that domain but you don't have to
learn a new language because learning
new stuff consumes mine budget and it's
difficult
before coming to go I used to write in
Java so I know Java quite well and I
want to write web front-ends well you
can write web front-ends in Java it's
not impossible but it's like using that
to pull out nails so the point I'm
trying to say
the final point I'm trying to make is
you need to have the right tools so what
is goal good for so hammer is good for
hammering in stuff that cow hammer is
not good for pulling out nails go is
good for writing cloud services back-end
service it's good for being well this is
debatable but those people who have
written in go think it's actually quite
simple to read and understand it's good
for having this thing funny thing called
implicit interface satisfaction which
I'll talk about in a while it's got
fantastic concurrency and my favorite
feature its Java done right it's
cross-platform
Sogo is somewhat familiar that's the
first line hello will in go if you
ignore the funny fmt in the capital case
it's not a class print line it just goes
way of denoting a public variable you
see it's pretty much the same across
many many different languages so goal is
somewhat familiar and therefore somewhat
easy to learn in fact go about design to
be easy to learn anybody who comes from
a Java environment or c-lite language
will find go somewhat familiar go is
definitely type safe I like to I like to
put up this example do you think this
come this program will run do you think
this program will compile really compile
it won't even compile let's try how many
of you have run your presentations
before I'm going to run the presentation
and let's read their messages go error
messages are not too bad actually
so what it's done is taken this HTML fed
it into a temporary file called compiled
0 dot go and the compiler complained
cannot use B type string in an integer
for assignment can you do in JavaScript
yes or no yes let's prove it and you put
down a microphone
so I is definitely okay thank you my
anything so I is definitely an integer
now I is a string
javascript is bad truffles I'm starting
a flame war here actually not true
javascript is a pretty good language now
in a godo so let's talk about go alright
this one is not so obvious how strongly
typesafe is go
why type safety at all but let's let's
look at this code and let's see whether
it will compile at all
I declare a type called mynum as an
integer and over here I declare a
variable I it's of type my num and I
assign it the value two over here I
assign a variable J of type int integer
of value three can I assign I and J in
Java cannot can in ogs definitely can
bingo
No let's see the error message some of
you already guessed answer's no let's
look at error message
cannot use type J cannot use J type
integer as type mine um so my num
even though was derived from an integer
is a separate typing goal so it is very
very very strongly typed and this is
very useful if you write big code if you
write code that is shared with loads of
programmers it's also very useful for
code you have written years ago this is
the equivalent of keeping everything in
its right place and the right place for
everything but there's always two sides
to a coin keeping everything in the
right place takes a lot of time a lot of
bookkeeping nodejs
allows you to throw things all the place
a teenager's bedroom nodejs is like a
teenager's bedroom go is like a
well-kept well-groomed house so strong
static typing allows you to catch most
errors at compile time and the runtime
is quite predictable in other words
after compiling and you deploy
production you can expect predictable
performance but the flip side of it is
if you are trying to throw a no-show to
get a short script a language like
nodejs Python or Ruby is much more
appropriate so guess the message is what
do you want to do what you already know
and what you want to do in the future so
decide whether you want to learn go let
me try to convince you that go is worth
learning so go has implicitly satisfied
interfaces those come in from Java know
about Java interfaces go tries to keep
keeping a house in order
static typing strong static typing more
pleasant so over here I declare an
interface type it's called one D fire
and it's got one
that in it called quantify and quantify
it takes nothing and returns an integer
so over here I declare another type my
SDR or my string it's derived from type
string and it also has a quantify method
now how do I know there is a method this
funny syntax which go created the way
you read this line means quantify is a
method of s of type my string which
returns an integer that's how you read
it so basically it's a matter of my
string it says print that means printer
screen quantifying s and was s its
length of s it returns the length of s
as the funny has a final value this is
just a side effect you just print out
the string now I declare another type
called
my int and it's derived from integer and
to quantify this integer I did tell me
whether it's positive negative or zero
that simple code for that so let's cut
to the chase
well this code were let's go to line by
line
that's just a print statement this line
is interesting
oh by the way go has this funny reversed
declaration syntax so a variable Q is of
type quantifier that's why it means it
is not quantifier is of type 2 it's the
other way around so variable Q is of
type quantifier so the quantifier has a
quantified method Q equals my int 3 now
that's another funny go Lang go language
syntax all he's saying is victory and
make it my it so I'm casting 3 to my end
think of it like a constructor in Java
so that's a constructor kind of thing
print queue 25 so 3 is a numeric it's
positive so should be 1 I guess so
Q equals my string ABC and the length of
ABC is 3 so we run this program the
first one should be 1 the second one
should be 3 let's check it out and sure
enough it is 1 in 3 now
let me edit my presentation and rerun it
anybody learn how to edit your
presentation and rerun it let's change
this to a minus four let's editing my
presentation this is written in go by
the week
real cool stuff and let's run it again
so now it shows up as minus one can
everybody at a backseater bottom rows
good now no surprise if I change it to
zero it should be zero let's run it and
it shows up as zero if I change this to
a four letter word maybe a five letter
word it just returns the length of the
string now I'm going to show you goes
see you routes anybody know see here
good what do you think will happen here
0x20 a see that's a hexadecimal number
what do you think what happened you
volunteers shout out your answer really
will one character well let's try it on
quantifying euro 0x20 AC is the Unicode
for Euro the Euro symbol and the Euro
symbol is a single character string and
it says 3 because the signal character
string is actually an integer and it's
of length 3 really confusing stuff but
here are the things they will trip you
up it's sea routes
alright why is this useful why are these
car interfaces useful now let's say I
have another type call exam paper and
exam paper you submitted it as Syrian
Dom I shouldn't use Microsoft should use
Google stuff anyway that's the URL for
my exam paper submission and let's say
exam paper is to create me quantify how
do you test this car stuff it's
difficult right it's gonna make a
service call go out to the Internet
grab the real exam paper and try to test
it and that's not very predictable the
internet connection may be down I may
have changed my exam my exam submission
but I can have fake exam paper to HTTP
does not matter dot see you in the doc
it never makes the internet call but
fake exam paper because it
satisfies quantify it works and it's
great for testing so this is how I use
interfaces all the time but this way of
defining interfaces is very very useful
because in Java or similar languages you
must say this class implements this and
you may find that the implements may be
implements ABCD efg until Zach a very
very long string of implements in go
there is no implements cuit it's
implicit implementation so long as it's
got a quantifier method it works the
most fantastic example of this is IO
reader and IO writer you can print line
to a database because it I all right I
will read ok so that is the second
reason why go is such a good language if
I'm losing any of you if you want us any
questions just shout out interrupt me
yes
so Java has if you have a functional
interface yeah Java it onwards has
function interfaces and the question is
if you use the functional interface of
Java you have this kind of capability
sort of right and how is it different
from the Java interface well I like to
think go start the refers Java copied it
but here that's actually a very good
question
go JavaScript and I'm sorry
leave go office JavaScript nodejs Java
Kotlin all the new modern languages if
you hear one of the authors of go speak
his name is Robert by interesting
speaker he says all these languages are
becoming more and more alike they're
getting more and more features Java is
getting functional features node.js is
trying to get classes so all these
languages are converging more or less
the same language perhaps it differs
slightly different syntax who is
different go is very very limited I'd
want to use this word but it is quite
relevant limited stupid basic language
it is designed so that anybody coming
from a C language can look at it and
solve kind of understand it it's
designed so that you can write code that
is efficient that compiles quickly and
when you look at here five years later
you can still understand it so goal is
designed somewhat like Python what
design Python was designed to be clear
and easily readable so many people have
actually come from Python to go
why because Python is really slow global
interpreter lock
I came to go not from Python but from
Ruby the rubies are fantastic language
it makes developers like me who write in
Ruby very happy it's a very very fun
language but but but yes group global
interpreter lock it's really slow but
that wasn't my issue it was fast enough
Ruby was fast enough for me but Ruby
code because it was so fun to write when
you look at it five years later what am
I trying to write here go tries to
circumvent that problem by anyway let's
go to goes the feature which I want to
talk about which is concurrency which is
something which for a person new to
programming finds it really hard and for
person who does JavaScript finds it
quite hard as well so over here I
declare a function let's call plus and
all it does for event however prints
the plus sign that's all it does after
one second it Blin prints another plus
forever and ever
here we have basically the same routine
but it brings out a period brings out a
dot okay
most of us when we start out writing
code we write code like this this this
one compiled so
let's run it like this this one compiled
it all it's got some real error message
about to execute select so I'm about to
execute select fatal all goroutines are
sleep deadlock which means there is
something in NGO called a goroutine and
his detected deadlock and its crash the
program and it's exited so okay let's
make the program work
let's fix me ok let's run + let's run
this program ok pluses are running but
my aim is to run + and go at the same
time concurrently so as a beginner
programmer I would do that of course
do you think that will never work
no why
a sequential execution plus is holding
on to the CPU I just want to let go
let's prove it yes plus is hogging the
CPU doesn't want to let go it doesn't
let go any time to run it all okay I am
the creator I'm the right of this
program now dot can run correct yeah
don't runs by one go and plus and dot to
run at the same time how do I do that
anybody we got go experts here come on
ah yes the goalkeeper do ah start a go
routine let's do that
I added three characters g/o and a space
and I've turned a regular routine into a
go routine in the routine that doesn't
block let's prove it hey it doesn't
block it works but hold on this part the
code is not executing this particle
which says about to execute select is
not executing how do I get that to
execute as well
oh you guys are learning fast
put another make dot a go routine so
let's do that
bang now there's something interesting
here this comes from printing lying go
concurrency this comes from about to
execute select and then the pluses and
dots what is actually happened here is
the program ran put that into the
background it's running independently in
the background put that in a background
that took a few hundred microseconds
fraction of a millisecond so in a
fraction of a millisecond or maybe a
millisecond later the print line
executed and I needed to put this select
here select here select is a statement
which actually tries to determine which
grow routine is ready to run but if I
remove that select if I remove the wait
forever what do you think will happen
the program would actually the program
would terminate
what happened to my goal routines if
main terminates the core routines
dominate let's prove that
so no more select it's commented out and
by the way the double slash if you've
not figured it out its comment
program exited no dots no plus so that's
another feature of go
it has garbage collected the goal
routines goal is a garbage collector
language with a very very very
performant garbage collector those
people coming from Java every now and
then when the garbage collector collects
garbage it stops the world the whole
system stops go garbage collector used
to be like that as well stop the world
for a few hundred milliseconds the
latest version or go stops the world for
a few hundred microseconds so it doesn't
really stop the world alright
those people who know JavaScript will
know no js' that's nothing new
no Jas has concurrency as well so let's
prove it I'll put it here
f12 to get console
so over here I've defined two JavaScript
functions function plus and function dot
and this is very JavaScript every 1000
milliseconds lakh to the console a plus
every 1000 milliseconds log to the
console a - no - a dot so when I run
that well pluses go on to pluses four
pluses five pluses go on and on
if I run dot if I can type
now you got plus and dot's so javascript
can do what go to us mind budget if you
know JavaScript do you want to learn go
okay was it the same sort of sort of the
same what I just did in my in my web
browser was I made use of a web worker
which will consume this is roughly
equivalent to one java thread which is
roughly equivalent to one operating
system-level thread so if my computer
has four cores it can run four threats
at the same time at the same time you
can have many many threads like an when
you run for them at the same time a go
routine is not blind the JavaScript web
worker know a goroutine well a million
go routines can share one thread
somebody actually did a demo a million
go routine share one thread so it's a
very simple very lightweight go routine
almost does nothing but still a million
go routines can say one thread and on a
fork or machine you get four million go
routines so the next point I'll make is
ah after Java II guess guess who the
coffee
guess who do you copy the coffee go the
ideas are very old the ideas are very
very old right goroutine is actually not
the correct word to use it's correct in
go by it came from paper called
concurrently concurrent sequential
processes it's called co-routines
spelled with a c' nobody paid attention
to co-routines except for the line for a
long long time
right then goal came out and say
co-routines are cool and I'm gonna brand
them goroutines and then everybody
started saying hmm Cortland once it as
well
Java wants it as well so all these
languages are becoming known as the same
language with different syntax they're
getting features before coming to go yes
Rudy as a Jordi no I programmed in Java
and I was looking for better Java so I
thought Scala is good
Scala is an extremely powerful language
don't get me wrong it can do everything
you can write dsls domain-specific
languages in Java it is so big that I
can't read my code after one week I can
be so undisciplined there I cannot read
my own code after one week that is color
anyway
how good is gos concurrency that's
that's another thing I want to highlight
about goal is course concurrency good go
is design the birthplace of goal was
actually Google we are at its birthplace
the organization not here but
organization and goal was invented the
story goes when you're trying to compile
a big C++ program that big C++ program
took about 40 minutes to compile they
compile the equivalent program in go and
it took less than a minute few seconds
but compiled time is one thing how
performin is it this is a whole web
browser in go now those people working
in no job no GSA say I can have Express
and I can write a similarly short web
browser in three lines or four lines or
two lines
let's besides the point the point is can
no GS perform on my really crappy laptop
11,000 requests per second with almost
no optimization well go optimization
compared with nginx same laptop running
a static website all it does is serve
hello well it came out with about the
same 10,000 requests per second 11,000
requests the second it's within the
arrow pens right what I'm trying to say
here is for beginner who knows nothing
about OS tuning who knows nothing about
concurrency you can write code that's
production-ready
as good as engineer
so goal is designed for heavy-duty
back-end service production-ready
back-end service a really good really
performant easy to read easy to extend
back-end service and my favorite feature
of all Java done right if you use pure
go in other words you don't link against
the C library you can link go against a
CLI breathe that's called C go if we
don't use Seco or use Windows specific
dll's or Mac specific libraries you can
all you need to do to cross compile the
Gnostic oh you go enable equals zero
turn off Seco specify an operating
system windows I've got clients that
insist on using Windows what they do no
near windows old windows 386 windows now
x64 go Buell hello well it generates an
exe file a windows 386 exe file let's
look at the complete list
Hayek's
Wow Android Darwyn what's dragonfly
don't know what dragonfly is FreeBSD
some funny always called illumos I don't
know about it
ah this is really really really
interesting javascript was a web
assembly yes cool can compile to the
JavaScript web assembly binaries and
these people are using it because they
want to play games on the web browser
and go a so performin they put a whole
game engine in the web assembly with
with physics and everything in there
nice target before go is just another
target it targets all these platforms my
platform happens be Linux AMD 64 so
let's go back another presentation
if you won a coat download the whole
presentation download its there soohyun
github lots of other presentations there
as well but this one is there and you
can run your presentation like I ran my
presentation thanks for listening thank
you questions comments flamewars yes
okay the question was usually
machine-learning folks used something
like Python and Python is a fantastic
language for machine learning because
Python was written by dude over in
Russell my pronouncing that name all
wrong
he's a mathematics professor so before
tensorflow came out you had numpy and
Python was an absolutely great language
in that area go is not it's not a
machine language optimized language is
the wrong tool to use go for machine
language but go is very very good for
data engineering before you can do
machine learning you need to get your
data in shape you need to clean your
data so go has data flow libraries which
can clean up massive amounts of data
really quickly but not the actual
machine learning itself any other
questions comments reactions you like go
is it worth learning yes okay my friend
here has a difficult question for me he
promised me a difficult question doesn't
matter repeat your question yes
[Music]
okay okay I guess I guess you can
summarize the question is summarized the
question is can you share pine can you
share memory effectively if you got
sixty instances of the same thing can
you share memory sort of kind of it will
eat up it will be 60 times the memory
okay
so let's answer actually is a very very
good question
let me attack let me attack the first
question first let me address the first
question first go binaries can get
pretty big pretty fast that hello world
was about one to two megabytes why is it
so big why is a hello world almost two
megabytes
yes when you write a Java class
HelloWorld in Java class and the class
file is a few hundred bytes a go
executable is a few megabytes almost two
megabytes but the Java file can run on
its own you need the JVM which is 50
megabytes or more the last time I
checked the entire JVM for go the entire
go runtime is embedded in the binary the
entire go runtime the handles go
routines the entire go runtime that does
all this smart implicit interfaces
entire go runtime that does strong
static types is embedded in that one or
two megabytes in the hello world so most
go binaries are self-contained most HTTP
is not self-contained but you can make
it self-contained and can make a pure
coding net HTTP or the net library
actually makes use of the operating
systems resolver library which means
it's a time I'm a clear link library and
many pure go operas don't like it so
they turn off see go and you can produce
a pure goal binary and that consumes
maybe about five megabytes to eight
megabytes the entire web browser
resolver stack so that's why gold
binaries can get big quite fast the
entire docker binary was written in go
and it's about fifty megabytes I think
right so really really huge projects
like docker kubernetes cube control cube
CTL they win roughly tens of megabytes
when you deploy a goal binary in a
docker container I don't use Alpine
because I guess I just used from scratch
in docker terms from scratch means start
with an empty container empty docker
container and in my go binary is five
megabytes my docker containers five
megabytes really tiny docker containers
so the mantra for goal is don't
communicate by sharing memory right
don't communicate by sharing memory
share memory by communicating so
everything in goal is independent and
unshared non share you communicate
across different processes using
channels any other comments questions
reactions if not I think Xiao Zhang yes
yes I've used a patch a benchmark to
test the let me repeat the question for
the audience the question was is go now
because that hello world was static
content static web browsers everybody
knows it's blazingly fast right any any
person can write a fast static web
browser the question was is go the right
tool for IO intensive dynamic content
and absolutely that's the purpose of
goal it's designed to have very very
fast io intensive stuff if if everything
is compute intensive like video encoding
go will not be very effective because
the compute or CPU resource hungry
process will grab the CPU but if it is
IO intensive go is the perfect fit much
much much better than no GS know GS
rocked the world because it multiplexed
a lot of i/o intensive process
on two one thread go multiplex the same
thing actually cause some copy no GS in
their sense but knock not one trip on
all available threads if you have a 64
core CPU 64 processes can be called the
task versus one process in no js' and
yet that yes ok take two days
I'll just because need ok
go good for writing back in service
. go is fantastic for writing back-end
service second question is cool
resource-hungry does go require a lot of
resources like Java for example in the
answer is no go is very very efficient
next to see in terms of efficiency if C
was the ghost and there is one it uses
100% go would use maybe of 110% maybe
hundred five percent so it is very very
resource efficient thank you
[Applause]