<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:atom="https://www.w3.org/2005/Atom">
  <channel>
    <title>Santiago Arias's blog</title>
    <description>Santiago Arias's blog</description>
    <link>https://www.sanarias.com/blog</link>
    <atom:link href="https://www.sanarias.com/blog/rss" rel="self" type="application/rss+xml" />
    <lastBuildDate>Thu, 15 Dec 2016 13:32:38 -0000</lastBuildDate>
    <pubDate>Thu, 15 Dec 2016 13:32:38 -0000</pubDate>
    <ttl>1800</ttl>
    
    <item>
      <title>A simple gRPC demo in Go and C#</title>
      <description>&lt;h3&gt;&lt;a href=&#34;/blog/1216AsimplegRPCdemoinGoandCSharp&#34; class=&#34;post-link&#34;&gt;A simple gRPC demo in Go and C#&lt;/a&gt;&lt;/h3&gt;
&lt;h4&gt;&lt;small&gt;Dec 15, 2016&lt;/small&gt;&lt;/h4&gt;
&lt;p&gt;I heard about gRPC some months ago and decided to learn a bit about it. Here is a collection of the information I found about it as well as a simple gRPC demo that uses &lt;strong&gt;Go&lt;/strong&gt; and &lt;strong&gt;C#&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;I created a github repo &lt;a href=&#34;https://github.com/santiaago/grpc.demo&#34;&gt;grpc.demo&lt;/a&gt; with all the code in this article.&lt;/p&gt;&lt;br&gt;&lt;h4&gt;Table of contents&lt;/h4&gt;&lt;ul&gt;&lt;li&gt;gRPC&lt;/li&gt;&lt;li&gt;Protocol Buffers&lt;/li&gt;&lt;li&gt;A gRPC example&lt;ul&gt;&lt;li&gt;Installation-process&lt;/li&gt;&lt;li&gt;The proto file&lt;/li&gt;&lt;li&gt;gRPC Go Server&lt;/li&gt;&lt;li&gt;gRPC Go Client&lt;/li&gt;&lt;li&gt;gRPC C# Client&lt;/li&gt;&lt;li&gt;gRPC dotnet core Client&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;What else&lt;/li&gt;&lt;li&gt;Resources&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;&lt;h2 id=&#34;grpc&#34;&gt;gRPC&lt;/h2&gt;&lt;p&gt;gRPC is an open source RPC library from google. It is an alternative to REST for microservices. It is based on the HTTP2 standard, and uses protocol buffers (Proto3).&lt;/p&gt;&lt;p&gt;gRPC is available in many languages, some of them have there own implementation (C, Go, Java) and some a wrapper around the C implementation so you are not tied to any language.&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;gRPC extends the Go programming model over the network.It is an excellent fit for building parallel, distributed, and streaming systems.&lt;a href=&#34;https://www.youtube.com/watch?v=sZx3oZt7LVg&#34;&gt;Sameer Ajmani&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;gRPC aims to be more efficient than JSON/HTTP. It encodes data with more efficiency thanks to Protocol Buffers and HTTP/2 makes the transport faster.&lt;/p&gt;&lt;p&gt;You can use &lt;strong&gt;Unary RPC&lt;/strong&gt; (request, response) or &lt;strong&gt;Streaming RPC&lt;/strong&gt; (send one or more messages).&lt;/p&gt;&lt;p&gt;To learn more about the motivations behind it you should read this web page: &lt;a href=&#34;http://www.grpc.io/blog/principles&#34;&gt;gRPC Motivation and Design Principles&lt;/a&gt;&lt;/p&gt;&lt;br&gt;&lt;h2 id=&#34;protocol-buffers-&#34;&gt;Protocol Buffers:&lt;/h2&gt;&lt;p&gt;Protocol buffers are used to define a mechanism to serialize structured data. You define the structure of the data (messages) and a service that you want to use to communicate. Then generate the source code for the message(s) and service(s) you defined to use in the server or client.&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;Multiple applications written in different programming languages can exchange a large number of messages quickly and reliably without overloading the network. &lt;a href=&#34;http://www.minaandrawos.com/2014/05/27/practical-guide-protocol-buffers-protobuf-go-golang/&#34;&gt;Practical guide to protocol buffers&lt;/a&gt;&lt;/p&gt;&lt;/blockquote&gt;&lt;br&gt;&lt;h2 id=&#34;a-grpc-example&#34;&gt;A gRPC example&lt;/h2&gt;&lt;p&gt;I wanted to build a simple example to test the &lt;strong&gt;Unary RPC&lt;/strong&gt; use case. Test how the exchange between different programming languages worked/felt.&lt;/p&gt;&lt;p&gt;I decided to create a &lt;strong&gt;Go server&lt;/strong&gt; and a &lt;strong&gt;Go client&lt;/strong&gt; to start. Then extend it to a &lt;strong&gt;C# client&lt;/strong&gt; that calls the same &lt;strong&gt;Go server&lt;/strong&gt; as before. And finally try a &lt;strong&gt;dotnet core client&lt;/strong&gt; as well.&lt;/p&gt;&lt;br&gt;&lt;h3 id=&#34;installation-process&#34;&gt;Installation process&lt;/h3&gt;&lt;p&gt;To use gRPC you will have to install &lt;code&gt;grpc&lt;/code&gt;, &lt;code&gt;protobuf&lt;/code&gt; and &lt;code&gt;protoc-gen-go&lt;/code&gt;.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;install grpc&lt;ul&gt;&lt;li&gt;&lt;code&gt;go get google.golang.org/grpc&lt;/code&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;install &lt;a href=&#34;https://github.com/google/protobuf/releases/tag/v3.0.0&#34;&gt;protocol buffers&lt;/a&gt;&lt;/li&gt;&lt;li&gt;install the go compiler plugin protoc-gen-go&lt;ul&gt;&lt;li&gt;&lt;code&gt;go get -u github.com/golang/protobuf/protoc-gen-go&lt;/code&gt;&lt;/li&gt;&lt;li&gt;&lt;code&gt;protoc-gen-go&lt;/code&gt; is a compiler to generate Go code from a proto file.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;&lt;h3 id=&#34;the-proto-file&#34;&gt;The proto file&lt;/h3&gt;&lt;p&gt;The proto file is where you define the gRPC service and the messages that will be used to communicate.&lt;/p&gt;&lt;pre class=&#34;prettyprint&#34;&gt;syntax = &#34;proto3&#34;;&lt;br&gt;&lt;br&gt;package reverse;&lt;br&gt;&lt;br&gt;service ReverseService {&lt;br&gt;    rpc ReverseString (ReverseRequest) returns (ReverseReply) {}&lt;br&gt;}&lt;br&gt;&lt;br&gt;message ReverseRequest {&lt;br&gt;    string data = 1;&lt;br&gt;}&lt;br&gt;&lt;br&gt;message ReverseReply {&lt;br&gt;    string reversed = 2;&lt;br&gt;}&lt;/pre&gt;&lt;p&gt;To generate the gRPC code run the following command.&lt;/p&gt;&lt;br&gt;&lt;pre  class=&#34;prettyprint&#34;&gt;&amp;gt;&amp;nbsp;...\grpc.demo&amp;gt;&amp;nbsp;protoc&amp;nbsp;-I&amp;nbsp;.\proto\&amp;nbsp;.\proto\reverse.proto&amp;nbsp;--go_out=plugins=grpc:proto&lt;/pre&gt;&lt;p&gt;This generates a &lt;code&gt;reverse.pb.go&lt;/code&gt; file that holds the &lt;code&gt;ReverseRequest&lt;/code&gt; and &lt;code&gt;ReverseReply&lt;/code&gt; messages types as well as the &lt;code&gt;ReverseService&lt;/code&gt; client and server.&lt;/p&gt;&lt;br&gt;&lt;h3 id=&#34;grpc-go-server&#34;&gt;gRPC Go server&lt;/h3&gt;&lt;p&gt;Create a &lt;code&gt;server&lt;/code&gt; type that implements the &lt;code&gt;ReverseString&lt;/code&gt; function and make the server serve the gRPC service:&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;package main&lt;br&gt;&lt;br&gt;import (&lt;br&gt;	&#34;log&#34;&lt;br&gt;	&#34;net&#34;&lt;br&gt;&lt;br&gt;	pb &#34;github.com/santiaago/grpc.demo/proto&#34;&lt;br&gt;&lt;br&gt;	&#34;golang.org/x/net/context&#34;&lt;br&gt;	&#34;google.golang.org/grpc&#34;&lt;br&gt;	&lt;br&gt;	&#34;google.golang.org/grpc/reflection&#34;&lt;br&gt;)&lt;br&gt;&lt;br&gt;const (&lt;br&gt;	port = &#34;:50051&#34;&lt;br&gt;)&lt;br&gt;&lt;br&gt;type server struct{}&lt;br&gt;&lt;br&gt;func (s *server) ReverseString(ctx context.Context, in *pb.ReverseRequest) (*pb.ReverseReply, error) {&lt;br&gt;	// your reverse string implementation here&lt;br&gt;	return &amp;pb.ReverseReply{Reversed: reversed}, nil&lt;br&gt;}&lt;br&gt;&lt;br&gt;func main() {&lt;br&gt;	lis, err := net.Listen(&#34;tcp&#34;, port)&lt;br&gt;	if err != nil {&lt;br&gt;		log.Fatalf(&#34;failed to listen: %v&#34;, err)&lt;br&gt;	}&lt;br&gt;	s := grpc.NewServer()&lt;br&gt;	pb.RegisterReverseServiceServer(s, &amp;server{})&lt;br&gt;	reflection.Register(s)&lt;br&gt;	if err := s.Serve(lis); err != nil {&lt;br&gt;		log.Fatalf(&#34;failed to server: %v&#34;, err)&lt;br&gt;	}&lt;br&gt;}&lt;/pre&gt;&lt;br&gt;&lt;h3 id=&#34;grpc-go-client&#34;&gt;gRPC Go client&lt;/h3&gt;&lt;p&gt;Create a Go client that dials the gRPC server. Get the client object and call &lt;code&gt;ReverseString&lt;/code&gt; on it.&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;package main&lt;br&gt;&lt;br&gt;import (&lt;br&gt;	&#34;log&#34;&lt;br&gt;&lt;br&gt;	pb &#34;github.com/santiaago/grpc.demo/proto&#34;&lt;br&gt;&lt;br&gt;	&#34;golang.org/x/net/context&#34;&lt;br&gt;	&#34;google.golang.org/grpc&#34;&lt;br&gt;)&lt;br&gt;&lt;br&gt;const (&lt;br&gt;	address = &#34;localhost:50051&#34;&lt;br&gt;)&lt;br&gt;&lt;br&gt;func main() {&lt;br&gt;	conn, err := grpc.Dial(address, grpc.WithInsecure())&lt;br&gt;	if err != nil {&lt;br&gt;		log.Fatalf(&#34;did not connect: %v&#34;, err)&lt;br&gt;	}&lt;br&gt;&lt;br&gt;	defer conn.Close()&lt;br&gt;	c := pb.NewReverseServiceClient(conn)&lt;br&gt;	r, err := c.ReverseString(context.Background(), &amp;pb.ReverseRequest{&lt;br&gt;		Data: &#34;Hello, world&#34;,&lt;br&gt;	})&lt;br&gt;&lt;br&gt;	if err != nil {&lt;br&gt;		log.Fatalf(&#34;could not greet: %v&#34;, err)&lt;br&gt;	}&lt;br&gt;	log.Println(&#34;Reversed string: &#34;, r.Reversed)&lt;br&gt;}&lt;/pre&gt;&lt;p&gt;Output:&lt;/p&gt;&lt;p&gt;server:&lt;/p&gt;&lt;pre class=&#34;prettyprint&#34;&gt;...\go.server&amp;gt;&amp;nbsp;go&amp;nbsp;run&amp;nbsp;.\main.go&lt;br&gt;2016/12/13&amp;nbsp;14:28:10&amp;nbsp;transport:&amp;nbsp;http2Server.HandleStreams&amp;nbsp;failed&amp;nbsp;to&amp;nbsp;read&amp;nbsp;frame:&amp;nbsp;read&amp;nbsp;tcp&amp;nbsp;[::1]:50051-&amp;gt;[::1]:1195:&amp;nbsp;wsarecv:&amp;nbsp;An&amp;nbsp;existing&amp;nbsp;connection&amp;nbsp;was&amp;nbsp;forcibly&amp;nbsp;closed&amp;nbsp;by&amp;nbsp;the&amp;nbsp;remote&amp;nbsp;host.&lt;/pre&gt;&lt;p&gt;client:&lt;/p&gt;&lt;pre class=&#34;prettyprint&#34;&gt;...\go.client&amp;gt;&amp;nbsp;go&amp;nbsp;run&amp;nbsp;.\main.go&lt;br&gt;2016/12/13&amp;nbsp;14:29:20&amp;nbsp;Reversed&amp;nbsp;string:&amp;nbsp;&amp;nbsp;dlrow&amp;nbsp;,olleH&lt;/pre&gt;&lt;br&gt;&lt;h3 id=&#34;grpc-csharp-client&#34;&gt;gRPC csharp client&lt;/h3&gt;&lt;p&gt;On a c# project install &lt;code&gt;Grpc.Core&lt;/code&gt;, &lt;code&gt;Grpc.Tools&lt;/code&gt; and &lt;code&gt;Google.Protobuf&lt;/code&gt; from &lt;strong&gt;NuGet&lt;/strong&gt;.&lt;/p&gt;&lt;p&gt;Then generate the c# classes using &lt;code&gt;protoc.exe&lt;/code&gt; and &lt;code&gt;grpc_csharp_plugin.exe&lt;/code&gt;&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;...\csharp.client&amp;gt;&amp;nbsp;.\packages\Grpc.Tools.1.0.1\tools\windows_x86\protoc.exe&amp;nbsp;-I..\proto&amp;nbsp;--csharp_out&amp;nbsp;.&amp;nbsp;--grpc_out&amp;nbsp;.&amp;nbsp;..\proto\reverse.proto&amp;nbsp;--plugin=protoc-gen-grpc=packages/Grpc.Tools.1.0.1/tools/windows_x86/grpc_csharp_plugin.exe&lt;/pre&gt;&lt;p&gt;This generates &lt;code&gt;Reverse.cs&lt;/code&gt; and &lt;code&gt;ReverseGrpc.cs&lt;/code&gt; files. Include them in your project.&lt;/p&gt;&lt;p&gt;You can now create a client that calls the &lt;strong&gt;Go server&lt;/strong&gt;&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;using System;&lt;br&gt;using Grpc.Core;&lt;br&gt;using Reverse;&lt;br&gt;&lt;br&gt;namespace csharp.client&lt;br&gt;{&lt;br&gt;    internal class Program&lt;br&gt;    {&lt;br&gt;        private static void Main()&lt;br&gt;        {&lt;br&gt;            var channel = new Channel(&#34;127.0.0.1:50051&#34;, ChannelCredentials.Insecure);&lt;br&gt;&lt;br&gt;            var client = new ReverseService.ReverseServiceClient(channel);&lt;br&gt;&lt;br&gt;            var reply = client.ReverseString(new ReverseRequest&lt;br&gt;            {&lt;br&gt;                Data = &#34;Hello, World&#34;&lt;br&gt;            });&lt;br&gt;&lt;br&gt;            Console.WriteLine(&#34;Got: &#34; + reply.Reversed);&lt;br&gt;&lt;br&gt;            channel.ShutdownAsync().Wait();&lt;br&gt;&lt;br&gt;            Console.WriteLine(&#34;Press any key to exit...&#34;);&lt;br&gt;            Console.ReadKey();&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;}&lt;/pre&gt;&lt;p&gt;Output&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;Got:&amp;nbsp;dlroW&amp;nbsp;,olleH&lt;br&gt;Press&amp;nbsp;any&amp;nbsp;key&amp;nbsp;to&amp;nbsp;exit...&lt;/pre&gt;&lt;br&gt;&lt;h3 id=&#34;grpc-dotnet-core-client-&#34;&gt;gRPC dotnet core client:&lt;/h3&gt;&lt;p&gt;The dotnet core client was simple to build as the generated code is the same as for c#.&lt;/p&gt;&lt;p&gt;This is what I did:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;create dotnet core project&lt;/li&gt;&lt;li&gt;add grpc dependencies&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Include this in your &lt;code&gt;package.json&lt;/code&gt; file:&lt;/p&gt;&lt;pre class=&#34;prettyprint&#34;&gt;&amp;quot;dependencies&amp;quot;:&amp;nbsp;{&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;&amp;nbsp;&amp;nbsp;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;quot;Google.Protobuf&amp;quot;:&amp;nbsp;&amp;quot;3.0.0&amp;quot;,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;quot;Grpc&amp;quot;:&amp;nbsp;&amp;quot;1.0.1&amp;quot;,&lt;br&gt;},&lt;/pre&gt;&lt;ul&gt;&lt;li&gt;generate csharp files (same as before).&lt;/li&gt;&lt;li&gt;create the client (you can use the same client code as before)&lt;/li&gt;&lt;li&gt;build&lt;/li&gt;&lt;li&gt;run&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Output:&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;...\dotnetcore.client&amp;gt;&amp;nbsp;dotnet&amp;nbsp;run&lt;br&gt;Project&amp;nbsp;dotnetcore.client&amp;nbsp;(.NETCoreApp,Version=v1.0)&amp;nbsp;was&amp;nbsp;previously&amp;nbsp;compiled.&amp;nbsp;Skipping&amp;nbsp;compilation.&lt;br&gt;Got:&amp;nbsp;dlroW&amp;nbsp;,olleH&lt;br&gt;Press&amp;nbsp;any&amp;nbsp;key&amp;nbsp;to&amp;nbsp;exit...&lt;/pre&gt;&lt;br&gt;&lt;h2 id=&#34;what-else-&#34;&gt;What else:&lt;/h2&gt;&lt;p&gt;There are some more things I would like to try in the near future.&lt;/p&gt;&lt;ul&gt;&lt;li&gt;streaming gRPC calls&lt;/li&gt;&lt;li&gt;deploy a demo app to Google Cloud Pub/Sub to see how the deployment experience is.&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;&lt;h2 id=&#34;resources-&#34;&gt;Resources:&lt;/h2&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&#34;http://www.grpc.io/&#34;&gt;grpc.io&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://www.grpc.io/blog/principles&#34;&gt;gRPC Motivation and Design Principles&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;https://github.com/grpc/grpc&#34;&gt;gRPC repo&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://www.grpc.io/docs/quickstart/go.html&#34;&gt;gRPC Go Quick start&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://www.grpc.io/docs/quickstart/csharp.html&#34;&gt;gRPC c# quickstart&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;https://www.youtube.com/watch?v=sZx3oZt7LVg&#34;&gt;GothamGo 2015: gRPC: Google&amp;#39;s high-performance, open-source RPC framework by Sameer Ajmani&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;https://github.com/golang/talks/tree/master/2015/gotham-grpc&#34;&gt;gotham-grpc demo code&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;https://cloudplatform.googleblog.com/2016/08/gRPC-a-true-Internet-scale-RPC-framework-is-now-1-and-ready-for-production-deployments.html&#34;&gt;gRPC: a true internet-scale RPC framework is now 1.0 and ready for production deployments&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;https://opensource.com/bus/15/3/google-grpc-open-source-remote-procedure-calls&#34;&gt;Google shares gRPC as alternative to REST for microservices&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;https://developers.google.com/protocol-buffers/docs/proto&#34;&gt;protocol buffers - Language Guide&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;https://github.com/golang/protobuf&#34;&gt;Go support for Google&amp;#39;s protocol buffers&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://www.minaandrawos.com/2014/05/27/practical-guide-protocol-buffers-protobuf-go-golang/&#34;&gt;Practical guide to protocol buffers&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;Announcing gRPC Alpha for Google Cloud Pub/Sub&#34;&gt;https://cloud.google.com/blog/big-data/2016/03/announcing-grpc-alpha-for-google-cloud-pubsub&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;&lt;p&gt;Follow me at &lt;a href=&#34;http://twitter.com/santiago_arias&#34;&gt;@santiago_arias&lt;/a&gt; to be notified about more posts like this.&lt;/p&gt;&lt;small class=&#34;pull-right&#34;&gt;Santiaago&lt;/small&gt;</description>
      <link>https://www.sanarias.com/blog/1216AsimplegRPCdemoinGoandCSharp/</link>
      <guid>https://www.sanarias.com/blog/1216AsimplegRPCdemoinGoandCSharp/</guid>
      <pubDate>Thu, 15 Dec 2016 13:32:38 -0000</pubDate>
    </item>
    
    <item>
      <title>2015 in review</title>
      <description>&lt;h3&gt;&lt;a href=&#34;/blog/3162015inreview&#34; class=&#34;post-link&#34;&gt;2015 in review&lt;/a&gt;&lt;/h3&gt;
&lt;h4&gt;&lt;small&gt;Mar 08, 2016&lt;/small&gt;&lt;/h4&gt;
&lt;p&gt;As &lt;a href=&#34;http://www.sanarias.com/blog/1132012inreview&#34;&gt;in&lt;/a&gt; &lt;a href=&#34;http://www.sanarias.com/blog/4142013inreview&#34;&gt;previous&lt;/a&gt; &lt;a href=&#34;http://www.sanarias.com/blog/12142014inreview&#34;&gt;years&lt;/a&gt;, I would like to make a review of the year 2015.&lt;br&gt;Here is my github contributions for this year, as my current company works on github, this pumps up the numbers :p.&lt;img src=&#34;http://www.sanarias.com/static/img/contributions_2015.png&#34; alt=&#34;git contributions 2015&#34;&gt;&lt;/p&gt;&lt;br&gt;&lt;h4&gt;tinygraphs:&lt;/h4&gt;&lt;p&gt;The main project that I finished in 2015 might have been &lt;strong&gt;tinygraphs&lt;/strong&gt;. A web app that generates consistent avatars with different themes. The app is available on &lt;a href=&#34;http://www.tinygraphs.com&#34;&gt;tinygraphs.com&lt;/a&gt;. The code is available at &lt;a href=&#34;https://github.com/taironas/tinygraphs&#34;&gt;github.com/taironas/tinygraphs&lt;/a&gt;. It is under the &lt;strong&gt;taironas&lt;/strong&gt; organization which I created to centralize apps that I code with friends. The app is done in &lt;a href=&#34;https://golang.org&#34;&gt;Go&lt;/a&gt; and the front end is done in &lt;a href=&#34;https://angularjs.org/&#34;&gt;angularjs&lt;/a&gt;. While doing this app I learned many things about developing web applications and wrote the following articles:&lt;ul&gt;&lt;li&gt;&lt;a href=&#34;http://www.sanarias.com/blog/415BuildingtinygraphsanavatarwebserviceinGo&#34;&gt;Building tinygraphs an avatar web service in Go&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://www.sanarias.com/blog/215TestingHTTPcachinginGo&#34;&gt;Testing HTTP caching in Go&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://www.sanarias.com/blog/115LearningHTTPcachinginGo&#34;&gt;Learning HTTP caching in Go&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://www.sanarias.com/blog/115BuildingachessboardimageinGo&#34;&gt;Building a chessboard image in Go&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://www.sanarias.com/blog/1214PlayingwithimagesinHTTPresponseingolang&#34;&gt;Playing with images in HTTP response in golang&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/p&gt;&lt;br&gt;&lt;h4&gt;route:&lt;/h4&gt;&lt;p&gt;I also built a minimal Go package with Remy Jourde called &lt;a href=&#34;https://github.com/taironas/route&#34;&gt;route&lt;/a&gt;. This package handles http routing in Go web applications. This package is currently used in &lt;a href=&#34;https://github.com/taironas/gonawin&#34;&gt;gonawin&lt;/a&gt; and &lt;a href=&#34;https://github.com/taironas/gonawin&#34;&gt;tinygraphs&lt;/a&gt; apps.&lt;/p&gt;&lt;br&gt;&lt;h4&gt;idm:&lt;/h4&gt;&lt;p&gt;I started coding &lt;a href=&#34;https://github.com/santiaago/idm&#34;&gt;idm&lt;/a&gt; (&lt;strong&gt;i&lt;/strong&gt;t &lt;strong&gt;d&lt;/strong&gt;oesn&amp;#39;t &lt;strong&gt;m&lt;/strong&gt;atter) an interpreted programming language based on &lt;a href=&#34;https://en.wikipedia.org/wiki/APL_(programming_language&#34;&gt;APL&lt;/a&gt;) and &lt;a href=&#34;https://github.com/robpike/ivy&#34;&gt;ivy&lt;/a&gt;.&lt;br&gt;This idea came after watching this two videos:&lt;ul&gt;&lt;li&gt;&lt;a href=&#34;https://www.youtube.com/watch?v=_DTpQ4Kk2wA&#34;&gt;APL demonstration 1975&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;https://www.youtube.com/watch?v=PXoG0WX0r_E&#34;&gt;Implementing a bignum calculator - Rob Pike&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;It is not finished and the code needs a lot of clean up but it does work and it was very fun to code :) .&lt;/p&gt;&lt;br&gt;&lt;h4&gt;machine learning things:&lt;/h4&gt;&lt;p&gt;One of the things I did during the past year was continue learning machine learning, a field that I am very passionate about. I followed the course &lt;a href=&#34;http://www.inference.eng.cam.ac.uk/mackay/itila/&#34;&gt;Information Theory, Inference, and Learning Algorithms&lt;/a&gt;, the lectures available &lt;a href=&#34;http://videolectures.net/david_mackay/&#34;&gt;online&lt;/a&gt;.&lt;br&gt;I started to compete on simple machine learning problems in &lt;a href=&#34;https://www.kaggle.com/&#34;&gt;kaggle&lt;/a&gt;. I decided to have a repository to centralize the different competitions &lt;a href=&#34;https://github.com/santiaago/kaggle&#34;&gt;santiaago/kaggle&lt;/a&gt;. For this I set some conditions/challenges. First, all code had to be done in &lt;strong&gt;Go&lt;/strong&gt;. Second, I had to code all algorithms and not use any framework. This forced me to start my own machine learning framework &lt;a href=&#34;http://github.com/santiaago/ml&#34;&gt;github.com/santiaago/ml&lt;/a&gt; right now it has linear regression, logistic regressions, support vector machines and linear transformations. As I was coding a different method the results of the competition improved. I would like to code neural networks models, random forest and PCA, that should be fun.&lt;/p&gt;&lt;br&gt;&lt;h4&gt;Project wish list for 2016:&lt;/h4&gt;&lt;p&gt;As in previous years, this is what I wish to accomplish during this year:&lt;ul&gt;&lt;li&gt;&lt;a href=&#34;http://github.com/taironas/pomotime&#34;&gt;pomotime&lt;/a&gt;: is a web app to record your &lt;strong&gt;pomodoro&lt;/strong&gt; progress This app is on hold as I have too much on my plate right now. This is done in &lt;strong&gt;Go&lt;/strong&gt;, &lt;strong&gt;Dart&lt;/strong&gt; and &lt;strong&gt;mongodb&lt;/strong&gt;.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;xprize&lt;/strong&gt;: I am participating with a friend on a competition called &lt;a href=&#34;http://learning.xprize.org/&#34;&gt;xprize&lt;/a&gt;, we are building some apps for tablets to teach kids to read, write and count. If you want more information go to the web site listed above. This is done in &lt;strong&gt;C++&lt;/strong&gt; and &lt;strong&gt;cocos2d-x&lt;/strong&gt;. The deadline is around November 2016.&lt;/li&gt;&lt;li&gt;I am working with a friend on a very interesting &lt;strong&gt;machine learning&lt;/strong&gt; project that I cannot talk a lot about right now, but we should have a prototype ready for late 2016. This is currently done in python as most of the machine learning frameworks are in python.&lt;/li&gt;&lt;li&gt;finish a &lt;strong&gt;deep learning&lt;/strong&gt; course: I started a deep learning course by &lt;strong&gt;Udacity&lt;/strong&gt; and &lt;strong&gt;google&lt;/strong&gt;, the repository is available &lt;a href=&#34;https://github.com/santiaago/udacity.ud730.deeplearning&#34;&gt;here&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;strong&gt;ml framework&lt;/strong&gt;: keep working on the machine learning framework, add neural networks, PCA and random forest.&lt;/li&gt;&lt;li&gt;modify the ml framework to be in sync with other ml frameworks, mainly use the same terminology as other frameworks.&lt;/li&gt;&lt;li&gt;keep doing some interesting &lt;strong&gt;Kaggle&lt;/strong&gt; competitions.&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;Let&#39;s see what 2016 brings :-)&lt;br&gt;&lt;br&gt;Follow me at &lt;a href=&#34;http://twitter.com/santiago_arias&#34;&gt;@santiago_arias&lt;/a&gt; to be notified about more posts like this.&lt;/p&gt;&lt;small class=&#34;pull-right&#34;&gt;Santiaago&lt;/small&gt;</description>
      <link>https://www.sanarias.com/blog/3162015inreview/</link>
      <guid>https://www.sanarias.com/blog/3162015inreview/</guid>
      <pubDate>Tue, 08 Mar 2016 10:10:43 -0000</pubDate>
    </item>
    
    <item>
      <title>Learning to answer your own questions in Go</title>
      <description>&lt;h3&gt;&lt;a href=&#34;/blog/915LearningtoansweryourownquestionsinGo&#34; class=&#34;post-link&#34;&gt;Learning to answer your own questions in Go&lt;/a&gt;&lt;/h3&gt;
&lt;h4&gt;&lt;small&gt;Sep 15, 2015&lt;/small&gt;&lt;/h4&gt;
&lt;p&gt;At &lt;a href=&#34;http://golanguk.com/&#34;&gt;Golang Uk&lt;/a&gt; I learned from Andrew Gerrand&amp;#39;s &lt;a href=&#34;https://talks.golang.org/2015/tricks.slide#1&#34;&gt;talk&lt;/a&gt; that you can define a type inside a function.&lt;br&gt;I was curious if doing this would impact the performance of the code.&lt;br&gt;Later the same day I asked the guy that sat next to me that same question. Turns out that guy was Alan Donovan who is one of the authors of &lt;a href=&#34;http://golang.org/s/oracle-user-manual&#34;&gt;go oracle&lt;/a&gt;.&lt;br&gt;Alan told me:&lt;/p&gt;&lt;blockquote&gt;&lt;p&gt;Both codes compile to the same assembly code so it doesn&amp;#39;t matter if you define it inside or outside the function.&lt;/p&gt;&lt;/blockquote&gt;&lt;p&gt;Very interesting, I didn&amp;#39;t know that :).&lt;/p&gt;&lt;p&gt;The next week I was watching bradfitz talk &lt;a href=&#34;https://github.com/bradfitz/talk-yapc-asia-2015/blob/master/talk.md&#34;&gt;Profiling &amp;amp; Optimizing in Go&lt;/a&gt; and decided to use the methods he described to answer this same question on my own.&lt;/p&gt;&lt;p&gt;So this is what I did.&lt;/p&gt;&lt;p&gt;&lt;strong&gt;I want to answer this question:&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Does defining a type inside or outside a function impact the performance of my code?&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: I think it is interesting to understand how Go works under the hood. I was interested in using the methods described on Brad&#39;s talk to answer my own questions. I am aware that you wouldn&amp;#39;t normally bother about the performance implication of this trivial case.&lt;/p&gt;&lt;p&gt;&lt;img src=&#34;http://www.sanarias.com/static/img/diff_asm.png&#34; alt=&#34;diff of two assembly codes on the fn function&#34; class=&#34;img-polaroid&#34; width=&#34;400&#34;&gt;&lt;/p&gt;&lt;h4 id=&#34;start-with-a-simple-package-&#34;&gt;Start with a simple package:&lt;/h4&gt;&lt;p&gt;Let&amp;#39;s start with a very simple package &lt;code&gt;typo&lt;/code&gt; defined in &lt;code&gt;typo.go&lt;/code&gt; that has a function &lt;code&gt;fn&lt;/code&gt;.&lt;code&gt;fn&lt;/code&gt; defines a type, creates an object of that type then returns a string representation of this object.&lt;/p&gt;&lt;pre class=&#34;prettyprint&#34;&gt;package typo&lt;br&gt;&lt;br&gt;import &#34;fmt&#34;&lt;br&gt;&lt;br&gt;func fn() string {&lt;br&gt;	type T struct {&lt;br&gt;		id    int&lt;br&gt;		value string&lt;br&gt;	}&lt;br&gt;	t := T{0, &#34;hello&#34;}&lt;br&gt;}&lt;/pre&gt;&lt;h4 id=&#34;writing-a-benchmark-test-for-your-package-&#34;&gt;Writing a benchmark test for your package:&lt;/h4&gt;&lt;p&gt;In Go you can define normal tests if they start with the &lt;code&gt;Test&lt;/code&gt; word or benchmark test if they start with &lt;code&gt;Benchmark&lt;/code&gt; word. This is very useful to measure performance. In our case, we will compare the performance of the Benchmark test when the type is defined inside and outside the function &lt;code&gt;fn&lt;/code&gt;.&lt;br&gt;This is how you would create a benchmark test in a &lt;code&gt;typo_test.go&lt;/code&gt; file:&lt;/p&gt;&lt;pre class=&#34;prettyprint&#34;&gt;func BenchmarkFn(b *testing.B) {&lt;br&gt;	for i := 0; i &lt; b.N; i++ {&lt;br&gt;		fn()&lt;br&gt;	}&lt;br&gt;}&lt;/pre&gt;&lt;p&gt;If you run this benchmark test this is what you get:&lt;/p&gt;&lt;pre&gt;$ go test -v -run=^$ -bench=.&lt;br&gt;PASS&lt;br&gt;BenchmarkFn-8	 2000000	       648 ns/op&lt;br&gt;ok  	local/typo	1.970s&lt;/pre&gt;&lt;p&gt;This means the loop ran 2000000 times at 648 ns per loop.&lt;/p&gt;&lt;h4 id=&#34;running-the-pprof-profiler-&#34;&gt;Running the pprof profiler:&lt;/h4&gt;&lt;p&gt;Another tool that you can use is &lt;code&gt;pprof&lt;/code&gt;, to know more about it check this &lt;a href=&#34;http://blog.golang.org/profiling-go-programs&#34;&gt;link&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;You can run a &lt;strong&gt;cpu&lt;/strong&gt; profiler or a &lt;strong&gt;memory&lt;/strong&gt; profiler.&lt;/p&gt;&lt;p&gt;Let&amp;#39;s run the benchmark test with the &lt;code&gt;-cpuprofile&lt;/code&gt; flag:&lt;/p&gt;&lt;pre&gt;$ go test -v -run=^$ -bench=. -benchtime=2s -cpuprofile=prof.cpu&lt;br&gt;PASS&lt;br&gt;BenchmarkFn-8	 5000000	       659 ns/op&lt;br&gt;ok  	local/typo	3.981s&lt;/pre&gt;&lt;p&gt;The benchmark test generates a file &lt;code&gt;typo.test&lt;/code&gt; and with the &lt;code&gt;cpuprofile&lt;/code&gt; flag it creates the &lt;code&gt;prof.cpu&lt;/code&gt; file. With these files you can now run the &lt;code&gt;pprof&lt;/code&gt; tool to see where the time is spent when the benchmark test is run. Some useful commands are &lt;code&gt;top&lt;/code&gt;, &lt;code&gt;top --cum&lt;/code&gt;, &lt;code&gt;list yourFunctionName&lt;/code&gt;.&lt;/p&gt;&lt;pre&gt;$ go tool pprof typo.test prof.cpu&lt;br&gt;Entering interactive mode (type &#34;help&#34; for commands)&lt;br&gt;(pprof) top&lt;br&gt;3.80s of 3.85s total (98.70%)&lt;br&gt;Dropped 18 nodes (cum &lt;= 0.02s)&lt;br&gt;Showing top 10 nodes out of 34 (cum &gt;= 3.20s)&lt;br&gt;      flat  flat%   sum%        cum   cum%&lt;br&gt;     3.76s 97.66% 97.66%      3.76s 97.66%  runtime.mach_semaphore_wait&lt;br&gt;     0.03s  0.78% 98.44%      0.03s  0.78%  runtime.mCentral_Grow&lt;br&gt;     0.01s  0.26% 98.70%      0.03s  0.78%  fmt.(*pp).printReflectValue&lt;br&gt;         0     0% 98.70%      0.03s  0.78%  fmt.(*pp).doPrintf&lt;br&gt;         0     0% 98.70%      0.03s  0.78%  fmt.(*pp).printArg&lt;br&gt;         0     0% 98.70%      0.06s  1.56%  fmt.Sprintf&lt;br&gt;         0     0% 98.70%      0.06s  1.56%  local/typo.BenchmarkFn&lt;br&gt;         0     0% 98.70%      0.06s  1.56%  local/typo.fn&lt;br&gt;         0     0% 98.70%      0.67s 17.40%  runtime.findrunnable&lt;br&gt;         0     0% 98.70%      3.20s 83.12%  runtime.gcstopm&lt;br&gt;(pprof) top --cum&lt;br&gt;3.76s of 3.85s total (97.66%)&lt;br&gt;Dropped 18 nodes (cum &lt;= 0.02s)&lt;br&gt;Showing top 10 nodes out of 34 (cum &gt;= 3.20s)&lt;br&gt;      flat  flat%   sum%        cum   cum%&lt;br&gt;         0     0%     0%      3.79s 98.44%  runtime.schedule&lt;br&gt;         0     0%     0%      3.79s 98.44%  runtime.systemstack&lt;br&gt;         0     0%     0%      3.77s 97.92%  runtime.stopm&lt;br&gt;     3.76s 97.66% 97.66%      3.76s 97.66%  runtime.mach_semaphore_wait&lt;br&gt;         0     0% 97.66%      3.76s 97.66%  runtime.notesleep&lt;br&gt;         0     0% 97.66%      3.76s 97.66%  runtime.semasleep&lt;br&gt;         0     0% 97.66%      3.76s 97.66%  runtime.semasleep.func1&lt;br&gt;         0     0% 97.66%      3.76s 97.66%  runtime.semasleep1&lt;br&gt;         0     0% 97.66%      3.38s 87.79%  runtime.goschedImpl&lt;br&gt;         0     0% 97.66%      3.20s 83.12%  runtime.gcstopm&lt;/pre&gt;&lt;p&gt;In pprof you can see the time spent in function &lt;code&gt;fn&lt;/code&gt; by doing &lt;code&gt;list fn&lt;/code&gt;&lt;/p&gt;&lt;pre&gt;(pprof) list fn&lt;br&gt;Total: 3.85s&lt;br&gt;ROUTINE ======================== local/typo.fn in /Users/santiaago/Developer/go/src/local/typo/typo.go&lt;br&gt;         0       60ms (flat, cum)  1.56% of Total&lt;br&gt;         .          .      7:		id    int&lt;br&gt;         .          .      8:		value string&lt;br&gt;         .          .      9:	}&lt;br&gt;         .          .     10:&lt;br&gt;         .          .     11:	t := T{0, &#34;hello&#34;}&lt;br&gt;         .       60ms     12:	return fmt.Sprintf(&#34;%+v&#34;, t)&lt;br&gt;         .          .     13:}&lt;/pre&gt;&lt;p&gt;In the same way, you can also run the &lt;strong&gt;memory&lt;/strong&gt; profiler using the &lt;code&gt;-memprofile&lt;/code&gt; flag:&lt;/p&gt;&lt;pre&gt;$ go test -v -run=^$ -bench=. -benchtime=2s -memprofile=prof.mem&lt;br&gt;PASS&lt;br&gt;BenchmarkFn-8	 5000000	       660 ns/op&lt;br&gt;ok  	local/typo	3.991s&lt;/pre&gt;&lt;p&gt;If you run &lt;code&gt;pprof&lt;/code&gt; on the memory profile:&lt;/p&gt;&lt;pre&gt;$ go tool pprof typo.test prof.mem&lt;br&gt;Entering interactive mode (type &#34;help&#34; for commands)&lt;br&gt;(pprof) top&lt;br&gt;512.19kB of 512.19kB total (  100%)&lt;br&gt;Dropped 11 nodes (cum &lt;= 2.56kB)&lt;br&gt;      flat  flat%   sum%        cum   cum%&lt;br&gt;  512.19kB   100%   100%   512.19kB   100%  runtime.malg&lt;br&gt;         0     0%   100%   512.19kB   100%  runtime.mcommoninit&lt;br&gt;         0     0%   100%   512.19kB   100%  runtime.mpreinit&lt;br&gt;         0     0%   100%   512.19kB   100%  runtime.rt0_go&lt;br&gt;         0     0%   100%   512.19kB   100%  runtime.schedinit&lt;br&gt;(pprof) top --cum&lt;br&gt;512.19kB of 512.19kB total (  100%)&lt;br&gt;Dropped 11 nodes (cum &lt;= 2.56kB)&lt;br&gt;      flat  flat%   sum%        cum   cum%&lt;br&gt;  512.19kB   100%   100%   512.19kB   100%  runtime.malg&lt;br&gt;         0     0%   100%   512.19kB   100%  runtime.mcommoninit&lt;br&gt;         0     0%   100%   512.19kB   100%  runtime.mpreinit&lt;br&gt;         0     0%   100%   512.19kB   100%  runtime.rt0_go&lt;br&gt;         0     0%   100%   512.19kB   100%  runtime.schedinit&lt;/pre&gt;&lt;p&gt;You can also check the memory in &lt;code&gt;fn&lt;/code&gt; by doing &lt;code&gt;list fn&lt;/code&gt;.&lt;/p&gt;&lt;pre&gt;(pprof) list fn&lt;br&gt;Total: 512.19kB&lt;br&gt;ROUTINE ======================== local/typo.fn in /Users/santiaago/Developer/go/src/local/typo/typo.go&lt;br&gt;         0          0 (flat, cum)     0% of Total&lt;br&gt;         .          .      7:		id    int&lt;br&gt;         .          .      8:		value string&lt;br&gt;         .          .      9:	}&lt;br&gt;         .          .     10:&lt;br&gt;         .          .     11:	t := T{0, &#34;hello&#34;}&lt;br&gt;         .          .     12:	return fmt.Sprintf(&#34;%+v&#34;, t)&lt;br&gt;         .          .     13:}&lt;/pre&gt;&lt;h4 id=&#34;recording-your-results-&#34;&gt;Recording your results:&lt;/h4&gt;&lt;p&gt;Let&amp;#39;s record our results:&lt;/p&gt;&lt;pre&gt;$ go test -bench=. -memprofile=prof.mem | tee mem.0&lt;br&gt;PASS&lt;br&gt;BenchmarkFn-8	 2000000	       686 ns/op&lt;br&gt;ok  	local/typo	2.045s&lt;br&gt;&lt;br&gt;$ go test -bench=. -cpuprofile=prof.cpu | tee cpu.0&lt;br&gt;PASS&lt;br&gt;BenchmarkFn-8	 2000000	       672 ns/op&lt;br&gt;ok  	local/typo	2.045s&lt;/pre&gt;&lt;p&gt;We can now move the type definition outside of the function and see if something changes.&lt;/p&gt;&lt;pre class=&#34;prettyprint&#34;&gt;package typo&lt;br&gt;&lt;br&gt;import &#34;fmt&#34;&lt;br&gt;&lt;br&gt;type T struct {&lt;br&gt;	id    int&lt;br&gt;	value string&lt;br&gt;}&lt;br&gt;&lt;br&gt;func fn() string {&lt;br&gt;	t := T{0, &#34;hello&#34;}&lt;br&gt;	return fmt.Sprintf(&#34;%+v&#34;, t)&lt;br&gt;}&lt;/pre&gt;&lt;p&gt;And record these results as well:&lt;/p&gt;&lt;pre&gt;$ go test -bench=. -memprofile=prof.mem | tee mem.1&lt;br&gt;PASS&lt;br&gt;BenchmarkFn-8	 2000000	       638 ns/op&lt;br&gt;ok  	local/typo	1.933s&lt;br&gt;&lt;br&gt;$ go test -bench=. -cpuprofile=prof.cpu | tee cpu.1&lt;br&gt;PASS&lt;br&gt;BenchmarkFn-8	 2000000	       639 ns/op&lt;br&gt;ok  	local/typo	1.944s&lt;/pre&gt;&lt;h4 id=&#34;compare-your-results-&#34;&gt;Compare your results:&lt;/h4&gt;&lt;p&gt;you can compare the benchmarks with &lt;a href=&#34;https://godoc.org/golang.org/x/tools/cmd/benchcmp&#34;&gt;benchcmp&lt;/a&gt;:&lt;/p&gt;&lt;pre&gt;$ go get golang.org/x/tools/cmd/benchcmp&lt;/pre&gt;&lt;pre&gt;$ benchcmp mem.0 mem.1&lt;br&gt;benchmark         old ns/op     new ns/op     delta&lt;br&gt;BenchmarkFn-8     686           674           -1.75%&lt;br&gt;&lt;br&gt;$ benchcmp cpu.0 cpu.1&lt;br&gt;benchmark         old ns/op     new ns/op     delta&lt;br&gt;BenchmarkFn-8     672           691           +2.83%&lt;/pre&gt;&lt;p&gt;I don&amp;#39;t think these results are significant. So let&amp;#39;s try something else.&lt;/p&gt;&lt;h4 id=&#34;comparing-the-compiled-assembly-code-&#34;&gt;Comparing the compiled assembly code:&lt;/h4&gt;&lt;p&gt;Let&amp;#39;s compare the assembly codes of &lt;code&gt;fn&lt;/code&gt;. You can do this by doing &lt;code&gt;disasm fn&lt;/code&gt; inside &lt;code&gt;pprof&lt;/code&gt;.&lt;/p&gt;&lt;h5 id=&#34;before-&#34;&gt;Before:&lt;/h5&gt;&lt;pre&gt;(pprof) disasm fn&lt;br&gt;Total: 512.19kB&lt;br&gt;ROUTINE ======================== local/typo.fn&lt;br&gt;         0          0 (flat, cum)     0% of Total&lt;br&gt;         .          .      7daf0: GS MOVQ GS:0x8a0, CX&lt;br&gt;         .          .      7daf9: LEAQ -0x20(SP), AX&lt;br&gt;         .          .      7dafe: CMPQ 0x10(CX), AX&lt;br&gt;         .          .      7db02: JBE 0x7dc55&lt;br&gt;         .          .      7db08: SUBQ $0xa0, SP&lt;br&gt;         .          .      7db0f: XORL BX, BX&lt;br&gt;         .          .      7db11: MOVQ BX, 0xa8(SP)&lt;br&gt;         .          .      7db19: MOVQ BX, 0xb0(SP)&lt;br&gt;         .          .      7db21: XORL BX, BX&lt;br&gt;         .          .      7db23: XORL DX, DX&lt;/pre&gt;&lt;h5 id=&#34;after-&#34;&gt;After:&lt;/h5&gt;&lt;pre&gt;(pprof) disasm fn&lt;br&gt;Total: 512.19kB&lt;br&gt;ROUTINE ======================== local/typo.fn&lt;br&gt;         0          0 (flat, cum)     0% of Total&lt;br&gt;         .          .      7daf0: GS MOVQ GS:0x8a0, CX&lt;br&gt;         .          .      7daf9: LEAQ -0x20(SP), AX&lt;br&gt;         .          .      7dafe: CMPQ 0x10(CX), AX&lt;br&gt;         .          .      7db02: JBE 0x7dc55&lt;br&gt;         .          .      7db08: SUBQ $0xa0, SP&lt;br&gt;         .          .      7db0f: XORL BX, BX&lt;br&gt;         .          .      7db11: MOVQ BX, 0xa8(SP)&lt;br&gt;         .          .      7db19: MOVQ BX, 0xb0(SP)&lt;br&gt;         .          .      7db21: XORL BX, BX&lt;br&gt;         .          .      7db23: XORL DX, DX&lt;/pre&gt;&lt;p&gt;If you compare the two assembly codes of these functions you will find that they are identical. So it is true! That answers my question, defining types inside functions will not impact the performance in any way.&lt;/p&gt;&lt;h4 id=&#34;another-method-&#34;&gt;Another method:&lt;/h4&gt;&lt;p&gt;Another way to get to the same result (credit to Alan Donovan who told me about this) is to run the following command twice one with the type defined inside the function and one outside.&lt;/p&gt;&lt;pre&gt;$ go tool compile -S typo.go | sed -e &#39;s/\.go:[0-9]*//&#39; -e &#39;/^&#34;&#34;.init/q&#39; &gt;in.asm&lt;br&gt;$ go tool compile -S typo.go | sed -e &#39;s/\.go:[0-9]*//&#39; -e &#39;/^&#34;&#34;.init/q&#39; &gt;out.asm&lt;/pre&gt;&lt;p&gt;The sed script throws away line number information and truncates the disassembly at the start of the &lt;code&gt;init&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;You can then perform a diff between the two files.&lt;/p&gt;&lt;pre&gt;$ diff in.asm out.asm&lt;br&gt;@@ -29,7 +29,7 @@&lt;br&gt;        0x0085 00133 (typo)     MOVQ    $1, &#34;&#34;.autotmp_0002+120(SP)&lt;br&gt;        0x008e 00142 (typo)     MOVQ    $1, &#34;&#34;.autotmp_0002+128(SP)&lt;br&gt;        0x009a 00154 (typo)     MOVQ    BX, &#34;&#34;.autotmp_0002+112(SP)&lt;br&gt;-   0x009f 00159 (typo)     LEAQ    type.&#34;&#34;.T·1(SB), BX&lt;br&gt;+   0x009f 00159 (typo)     LEAQ    type.&#34;&#34;.T(SB), BX&lt;br&gt;        0x00a6 00166 (typo)     MOVQ    BX, (SP)&lt;br&gt;        0x00aa 00170 (typo)     LEAQ    &#34;&#34;.autotmp_0000+136(SP), BX&lt;br&gt;        0x00b2 00178 (typo)     MOVQ    BX, 8(SP)&lt;br&gt;@@ -98,7 +98,7 @@&lt;br&gt;        0x0160 e9 20 ff ff ff e8 00 00 00 00 e9 91 fe ff ff     . .............&lt;br&gt;        rel 5+4 t=13 +0&lt;br&gt;        rel 56+4 t=11 go.string.&#34;hello&#34;+0&lt;br&gt;-   rel 162+4 t=11 type.&#34;&#34;.T·1+0&lt;br&gt;+   rel 162+4 t=11 type.&#34;&#34;.T+0&lt;br&gt;        rel 193+4 t=5 runtime.convT2E+0&lt;br&gt;        rel 227+4 t=11 runtime.writeBarrierEnabled+-1&lt;br&gt;        rel 241+4 t=11 go.string.&#34;%+v&#34;+0&lt;/pre&gt;&lt;p&gt;The only difference between the two files is the type names, which are &lt;code&gt;T.1&lt;/code&gt; and &lt;code&gt;T&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;I hope these methods and go tools can help you answer your own questions.Go check bradfitz talk on &lt;a href=&#34;https://github.com/bradfitz/talk-yapc-asia-2015/blob/master/talk.md&#34;&gt;Profiling &amp;amp; Optimizing in Go&lt;/a&gt; if you haven&amp;#39;t already.&lt;/p&gt;&lt;p&gt;Follow me at &lt;a href=&#34;http://twitter.com/santiago_arias&#34;&gt;@santiago_arias&lt;/a&gt; to be notified about more posts like this.&lt;/p&gt;&lt;small class=&#34;pull-right&#34;&gt;Santiaago&lt;/small&gt;</description>
      <link>https://www.sanarias.com/blog/915LearningtoansweryourownquestionsinGo/</link>
      <guid>https://www.sanarias.com/blog/915LearningtoansweryourownquestionsinGo/</guid>
      <pubDate>Tue, 15 Sep 2015 12:39:55 -0000</pubDate>
    </item>
    
    <item>
      <title>Building a simple web app in Go Dart and Angular 2</title>
      <description>&lt;h3&gt;&lt;a href=&#34;/blog/615BuildingasimplewebappinGoDartandAngular2&#34; class=&#34;post-link&#34;&gt;Building a simple web app in Go Dart and Angular 2&lt;/a&gt;&lt;/h3&gt;
&lt;h4&gt;&lt;small&gt;Jun 16, 2015&lt;/small&gt;&lt;/h4&gt;
&lt;p&gt;I want to build a minimal web app using &lt;a href=&#34;http://golang.org/&#34;&gt;Golang&lt;/a&gt; for the backend and &lt;a href=&#34;https://www.dartlang.org/&#34;&gt;Dart&lt;/a&gt; and &lt;a href=&#34;https://angular.io/&#34;&gt;Angular 2.0&lt;/a&gt; for the frontend.&lt;/p&gt;&lt;p&gt;The Github repository of this app is &lt;a href=&#34;http://github.com/santiaago/gda2&#34;&gt;gda2&lt;/a&gt; (for &lt;b&gt;G&lt;/b&gt;olang &lt;b&gt;D&lt;/b&gt;art &lt;b&gt;A&lt;/b&gt;ngular2).&lt;/p&gt;&lt;p&gt;Here is the organization of this app:&lt;/p&gt;&lt;pre class=&#34;prettyprint&#34;&gt;GOPATH/src/github.com/santiaago/gda2 (master) tree&lt;br&gt;├── app&lt;br&gt;│   ├── pubspec.yaml&lt;br&gt;│   └── web&lt;br&gt;│       ├── index.html&lt;br&gt;│       ├── main.dart&lt;br&gt;└── backend&lt;br&gt;    └── main.go&lt;/pre&gt;&lt;h3&gt;The Backend&lt;/h3&gt;&lt;p&gt;The Go server will serve the client side files that the app needs to run. Also it will serve a minimal &lt;b&gt;api&lt;/b&gt; that returns a &lt;b&gt;json&lt;/b&gt; object.&lt;ul&gt;&lt;li&gt;request: &lt;code&gt;GET /api/hello&lt;/code&gt;&lt;/li&gt;&lt;li&gt;response: &lt;code&gt;{&amp;quot;Message&amp;quot;:&amp;quot;hello world&amp;quot;}&lt;/code&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;/p&gt;&lt;p&gt;Here is the handler &lt;code&gt;helloWorld&lt;/code&gt; that does that:&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;// helloWorld handler returns a json response with a &#39;Hello, World&#39; message.&lt;br&gt;//&lt;br&gt;func helloWorld(w http.ResponseWriter, r *http.Request) {&lt;br&gt;&lt;br&gt;    data := struct {&lt;br&gt;        Message string&lt;br&gt;    }{&lt;br&gt;        &#34;Hello, World&#34;,&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    if err := json.NewEncoder(w).Encode(data); err != nil {&lt;br&gt;        log.Println(err)&lt;br&gt;    }&lt;br&gt;}&lt;/pre&gt;&lt;p&gt;Now we need to serve that handler and serve the client side which is at &lt;code&gt;/app/web&lt;/code&gt;.&lt;/p&gt;&lt;pre class=&#34;prettyprint&#34;&gt;func main() {&lt;br&gt;    http.Handle(&#34;/&#34;, http.FileServer(http.Dir(&#34;./app/web/&#34;)))&lt;br&gt;&lt;br&gt;    http.HandleFunc(&#34;/api/hello&#34;, helloWorld)&lt;br&gt;    http.ListenAndServe(&#34;:8080&#34;, nil)&lt;br&gt;}&lt;/pre&gt;&lt;h3&gt;The frontend: Dart and Angular 2.0:&lt;/h3&gt;&lt;p&gt;We start from the code of the &lt;a href=&#34;https://angular.io/docs/dart/latest/quickstart.html&#34;&gt;quickstart&lt;/a&gt; project from &lt;a href=&#34;https://angular.io&#34;&gt;angular.io&lt;/a&gt;.&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;// These imports will go away soon:&lt;br&gt;import &#39;package:angular2/src/reflection/reflection.dart&#39; show reflector;&lt;br&gt;import &#39;package:angular2/src/reflection/reflection_capabilities.dart&#39; show ReflectionCapabilities;&lt;br&gt;&lt;br&gt;@Component(&lt;br&gt;  selector: &#39;my-app&#39;&lt;br&gt;)&lt;br&gt;@View(&lt;br&gt;  template: &#39;&amp;lt;h1&amp;gt;Hello &amp;lt;/h1&amp;gt;&#39;&lt;br&gt;)&lt;br&gt;&lt;br&gt;class AppComponent {&lt;br&gt;    String name = &#39;Alice&#39;;&lt;br&gt;}&lt;br&gt;main() {&lt;br&gt;  // Temporarily needed.&lt;br&gt;  reflector.reflectionCapabilities = new ReflectionCapabilities();&lt;br&gt;&lt;br&gt;  bootstrap(AppComponent);&lt;br&gt;}&lt;/pre&gt;&lt;p&gt;And the following &lt;code&gt;Index.html&lt;/code&gt;, also very similar to the quickstart code.&lt;/p&gt;&lt;pre class=&#34;prettyprint&#34;&gt;&amp;lt;!doctype html&amp;gt;&lt;br&gt;&amp;lt;html&amp;gt;&lt;br&gt;  &amp;lt;head&amp;gt;&lt;br&gt;    &amp;lt;title&amp;gt;Go Dart Angular2 app&amp;lt;/title&amp;gt;&lt;br&gt;  &amp;lt;/head&amp;gt;&lt;br&gt;  &amp;lt;body&amp;gt;&lt;br&gt;    &amp;lt;my-app&amp;gt;&amp;lt;/my-app&amp;gt;&lt;br&gt;    &amp;lt;script type=&amp;quot;application/dart&amp;quot; src=&amp;quot;main.dart&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br&gt;    &amp;lt;script src=&amp;quot;packages/browser/dart.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;br&gt;  &amp;lt;/body&amp;gt;&lt;br&gt;&amp;lt;/html&amp;gt;&lt;/pre&gt;&lt;p&gt;Now we want to change this code to be able to call the api that we just made.&lt;/p&gt;&lt;p&gt;To do that we can create a &lt;code&gt;Hello&lt;/code&gt; class and call &lt;code&gt;HttpRequest.getString&lt;/code&gt; in the constructor to set a variable value to the response of &lt;code&gt;/api/hello&lt;/code&gt;.&lt;/p&gt;&lt;pre class=&#34;prettyprint&#34;&gt;class Hello{&lt;br&gt;&lt;br&gt;  String message;&lt;br&gt;&lt;br&gt;  Hello(){&lt;br&gt;        HttpRequest.getString(&#39;/api/hello&#39;)&lt;br&gt;            .then((String content) {&lt;br&gt;              Map parsedMap = JSON.decode(content);&lt;br&gt;              message = parsedMap[&#34;Message&#34;];&lt;br&gt;            })&lt;br&gt;            .catchError((Error error) {&lt;br&gt;              print(error.toString());&lt;br&gt;            });&lt;br&gt;  }&lt;br&gt;}&lt;/pre&gt;&lt;p&gt;The next step is to create an instance of &lt;code&gt;Hello&lt;/code&gt; in &lt;code&gt;AppComponent&lt;/code&gt;&lt;/p&gt;&lt;pre class=&#34;prettyprint&#34;&gt;class AppComponent {&lt;br&gt;  Hello hello = new Hello();&lt;br&gt;}&lt;/pre&gt;&lt;p&gt;And then use it in the template:&lt;/p&gt;&lt;pre class=&#34;prettyprint&#34;&gt;@Component(&lt;br&gt;  selector: &#39;my-app&#39;&lt;br&gt;)&lt;br&gt;@View(&lt;br&gt;  template: &#39;&amp;lt;h1&amp;gt;&amp;quot;{{hello.message}}&amp;quot;&amp;lt;/h1&amp;gt;&#39;&lt;br&gt;)&lt;/pre&gt;&lt;p&gt;And that&amp;#39;s it! Now if we get the libraries, build and run the app:&lt;/p&gt;&lt;pre class=&#34;prettyprint&#34;&gt;&amp;gt; cd app&lt;br&gt;&amp;gt; pub get&lt;br&gt;&amp;gt; cd ..&lt;br&gt;&amp;gt; go get ./backend&lt;br&gt;&amp;gt;backend&lt;/pre&gt;&lt;p&gt;You can now open &lt;a href=&#34;https://www.chromium.org/Home&#34;&gt;Chromium&lt;/a&gt; you will see the following:&lt;/p&gt;&lt;p&gt;&lt;img src=&#34;http://www.sanarias.com/static/img/gda2.png&#34; alt=&#34;screenshot go dart angular 2.0&#34; class=&#34;img-polaroid&#34; &gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Here are some links that helped me understand this:&lt;/b&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&#34;https://angular.io/docs/dart/latest/quickstart.html&#34;&gt;Angular 2.0 dart quick start&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart:html.HttpRequest&#34;&gt;HTTP request class&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;https://www.dartlang.org/articles/json-web-service/&#34;&gt;Using Dart with JSON Web Services&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;I hope you have found this post useful, if you have any feedback, please let me know, I would be more than happy to improve the code and learn at the same time.&lt;/p&gt;&lt;p&gt;Follow me at &lt;a href=&#34;http://twitter.com/santiago_arias&#34;&gt;@santiago_arias&lt;/a&gt; to be notified about more posts like this.&lt;/p&gt;&lt;small class=&#34;pull-right&#34;&gt;Santiaago&lt;/small&gt;</description>
      <link>https://www.sanarias.com/blog/615BuildingasimplewebappinGoDartandAngular2/</link>
      <guid>https://www.sanarias.com/blog/615BuildingasimplewebappinGoDartandAngular2/</guid>
      <pubDate>Tue, 16 Jun 2015 15:00:38 -0000</pubDate>
    </item>
    
    <item>
      <title>Building tinygraphs an avatar web service in Go</title>
      <description>&lt;h3&gt;&lt;a href=&#34;/blog/415BuildingtinygraphsanavatarwebserviceinGo&#34; class=&#34;post-link&#34;&gt;Building tinygraphs an avatar web service in Go&lt;/a&gt;&lt;/h3&gt;
&lt;h4&gt;&lt;small&gt;Apr 14, 2015&lt;/small&gt;&lt;/h4&gt;
&lt;p&gt;I just finished a project called &lt;a href=&#34;http://tinygraphs.com&#34; target=&#34;_blank&#34;&gt;tinygraphs&lt;/a&gt; with the help of some &lt;a href=&#34;http://tinygraphs.com/##about&#34; target=&#34;_blank&#34;&gt;friends&lt;/a&gt;. You can find it on &lt;a href=&#34;http://github.com/taironas/tinygraphs&#34; target=&#34;_blank&#34;&gt;github.com/taironas/tinygraphs&lt;/a&gt;. It was built in &lt;a href=&#34;http://golang.org&#34; target=&#34;_blank&#34;&gt;Go&lt;/a&gt; and the website was done with &lt;a href=&#34;http://getbootstrap.com/&#34;  target=&#34;_blank&#34;&gt;Boostrap&lt;/a&gt; and &lt;a href=&#34;http://angularjs.org&#34; target=&#34;_blank&#34;&gt;AngularJS&lt;/a&gt; and designed by &lt;a href=&#34;https://plus.google.com/+CarmenRebolledo&#34; target=&#34;_blank&#34;&gt;+Carmen&lt;/a&gt;. This post is about how and why we built this.&lt;/p&gt;&lt;h3 id=&#34;the-idea&#34;&gt;The idea&lt;/h3&gt;&lt;p&gt;The idea came to life after we finished the first version of &lt;a href=&#34;http://github.com/taironas/gonawin&#34; target=&#34;_blank&#34;&gt;gonawin&lt;/a&gt;, a web app to make friendly bets on sport events. We built it to play during the &lt;strong&gt;Fifa World Cup&lt;/strong&gt; and needed some avatars for our users. We found an avatar generator built in &lt;strong&gt;Go&lt;/strong&gt; called &lt;a href=&#34;https://github.com/cupcake/sigil&#34; target=&#34;_blank&#34;&gt;Sigil&lt;/a&gt; and we used it for the avatars of our users and teams. After the World Cup came to an end we decided to build our own avatar generator web service. Our aim was to have a greater variety of color themes and patterns, other than the traditional square patterns.&lt;/p&gt;&lt;h3 id=&#34;the-patterns-&#34;&gt;The patterns:&lt;/h3&gt;&lt;p&gt;We started with the &lt;strong&gt;square&lt;/strong&gt; pattern, a 6x6 grid that is symetric in Y.&lt;img src=&#34;http://www.sanarias.com/static/img/tinygraphs_squares.png&#34; alt=&#34;squares&#34;&gt;&lt;div class=&#34;lead pagination-centered&#34;&gt;/squares/helloworld&lt;/div&gt;&lt;/p&gt;&lt;p&gt;We then moved towards something different. We decided to call it &lt;strong&gt;isogrids&lt;/strong&gt;. It is composed by small triangles and is symmetric in Y.&lt;img src=&#34;http://www.sanarias.com/static/img/tinygraphs_isogrids.png&#34; alt=&#34;isogrids&#34;&gt;&lt;div class=&#34;lead pagination-centered&#34;&gt;/isogrids/helloworld&lt;/div&gt;&lt;/p&gt;&lt;p&gt;The next pattern is a small derivation of the isogrid pattern, we called it &lt;strong&gt;hexa&lt;/strong&gt; in reference to the hexagon shape. It is basically a cleaner version of an isogrid avatar.&lt;img src=&#34;http://www.sanarias.com/static/img/tinygraphs_hexa.png&#34; alt=&#34;hexa&#34;&gt;&lt;div class=&#34;lead pagination-centered&#34;&gt;/lab/isogrids/hexa/helloworld&lt;/div&gt;&lt;/p&gt;&lt;p&gt;The &lt;strong&gt;hexa16&lt;/strong&gt; route is a great pattern, it is composed of 6 triangles that rotate to create an hexagon. Each triangle is a rotation of the previous one starting in the middle left.&lt;img src=&#34;http://www.sanarias.com/static/img/tinygraphs_hexa16.png&#34; alt=&#34;hexa16&#34;&gt;&lt;div class=&#34;lead pagination-centered&#34;&gt;/lab/isogrids/hexa16/helloworld&lt;/div&gt;&lt;/p&gt;&lt;p&gt;The &lt;strong&gt;spaceinvaders&lt;/strong&gt; pattern  was one of the funniest to code. It is a 10x10 grid. A space invader is defined by multiple parameters, number of antennas, number of eyes, size of arms, arms up or down, ...&lt;/p&gt;&lt;pre class=&#34;prettyprint&#34;&gt;type invader struct {&lt;br&gt;	legs        int&lt;br&gt;	foot        bool&lt;br&gt;	arms        int&lt;br&gt;	armsUp      bool&lt;br&gt;	anthenas    int&lt;br&gt;	height      int&lt;br&gt;	length      int&lt;br&gt;	eyes        int&lt;br&gt;	armSize     int&lt;br&gt;	anthenaSize int&lt;/pre&gt;&lt;p&gt;&lt;img src=&#34;http://www.sanarias.com/static/img/tinygraphs_spaceinvaders.png&#34; alt=&#34;spaceinvaders&#34; target=&#34;_blank&#34;&gt;&lt;div class=&#34;lead pagination-centered&#34;&gt;/spaceinvaders/helloworld&lt;/div&gt;&lt;/p&gt;&lt;p&gt;All the information needed to create these patterns is derived from the &lt;code&gt;key&lt;/code&gt; passed in the URL. In this case &lt;code&gt;helloworld&lt;/code&gt;. Let&amp;#39;s see how the avatars are generated.&lt;/p&gt;&lt;h3 id=&#34;how-the-avatars-are-generated&#34;&gt;How the avatars are generated&lt;/h3&gt;&lt;p&gt;We create an &lt;a href=&#34;http://en.wikipedia.org/wiki/MD5&#34; target=&#34;_blank&#34;&gt;MD5&lt;/a&gt; key from the string passed in the URL.&lt;/p&gt;&lt;p&gt;From this URL &lt;code&gt;http://tinygraphs.com/spaceinvaders/helloworld&lt;/code&gt; we extract the &lt;code&gt;helloworld&lt;/code&gt; identifier that generates an MD5 key: &lt;code&gt;fc5e038d38a57032085441e7fe7010b0&lt;/code&gt;.&lt;/p&gt;&lt;pre class=&#34;prettyprint&#34;&gt;// SpaceInvaders handler for /spaceinvaders/:key&lt;br&gt;func SpaceInvaders(w http.ResponseWriter, r *http.Request) {&lt;br&gt;	var err error&lt;br&gt;	var key string&lt;br&gt;	if key, err = route.Context.Get(r, &#34;key&#34;); err != nil {&lt;br&gt;		log.Println(&#34;Unable to get &#39;key&#39; value: &#34;, err)&lt;br&gt;		key = &#34;&#34;&lt;br&gt;	}&lt;br&gt;&lt;br&gt;	h := md5.New()&lt;br&gt;	io.WriteString(h, key)&lt;br&gt;	key = fmt.Sprintf(&#34;%x&#34;, h.Sum(nil)[:])&lt;br&gt;	// ...&lt;/pre&gt;&lt;p&gt;The generated key is an hexadecimal key. For each of the first 16 characters of the key we extract an information to create the avatar. In most cases each character is decoded into one of the colors that composes the avatar that we are building. In the case of the spaceinvaders pattern, each character of the key let us extract information such as, &lt;em&gt;how many eyes does this space invader has?&lt;/em&gt; or, &lt;em&gt;does he has antennas?&lt;/em&gt;, etc..&lt;/p&gt;&lt;h3 id=&#34;what-i-learned-while-building-tinygraphs-&#34;&gt;What I learned while building tinygraphs:&lt;/h3&gt;&lt;p&gt;Mostly how to work with images and how to use &lt;a href=&#34;https://github.com/ajstarks/svgo&#34; target=&#34;_blank&#34;&gt;svgo&lt;/a&gt; a Go Language Library for SVG generation. I have made some blog posts about some of the things I learned while building tinygraphs:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&#34;http://www.sanarias.com/blog/215TestingHTTPcachinginGo&#34; target=&#34;_bank&#34;&gt;Testing HTTP caching in Go&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://www.sanarias.com/blog/115LearningHTTPcachinginGo&#34; target=&#34;_bank&#34;&gt;Learning HTTP caching in Go&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://www.sanarias.com/blog/115BuildingachessboardimageinGo&#34; target=&#34;_bank&#34;&gt;Building a chessboard image in Go&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://www.sanarias.com/blog/1214PlayingwithimagesinHTTPresponseingolang&#34; target=&#34;_bank&#34;&gt;Playing with images in HTTP response in golang&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h3 id=&#34;hosting-a-go-web-app-on-heroku-&#34;&gt;Hosting a Go web app on Heroku:&lt;/h3&gt;&lt;p&gt;I had never properly used heroku before this. The development experience is great for a basic app (We do not even have a database :) ). There is a buildpack for Go &lt;a href=&#34;https://github.com/kr/heroku-buildpack-go/blob/master/bin/compile&#34; target=&#34;_bank&#34;&gt;here&lt;/a&gt; and here is an article to &lt;a href=&#34;http://mmcgrana.github.io/2012/09/getting-started-with-go-on-heroku&#34; target=&#34;_bank&#34;&gt;Get started with Go on Heroku&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;One of the great things that came with Heroku is that there is a github integration.&lt;/p&gt;&lt;p&gt;&lt;img src=&#34;http://www.sanarias.com/static/img/heroku-git.png&#34; alt=&#34;Heroku github integration&#34; target=&#34;_bank&#34;&gt;&lt;/p&gt;&lt;p&gt;You can configure it so that every push to &lt;code&gt;master&lt;/code&gt; triggers a build and is automatically deployed.&lt;/p&gt;&lt;p&gt;&lt;img src=&#34;http://www.sanarias.com/static/img/heroku-activity.png&#34; alt=&#34;Herkoku github activity&#34; height=&#34;300&#34; width=&#34;300&#34; target=&#34;_bank&#34;&gt;&lt;/p&gt;&lt;p&gt;I hope you have found this post useful.&lt;/p&gt;&lt;p&gt;Follow me at &lt;a href=&#34;http://twitter.com/santiago_arias&#34; target=&#34;_bank&#34;&gt;@santiago_arias&lt;/a&gt; to be notified about more posts like this.&lt;/p&gt;&lt;small class=&#34;pull-right&#34;&gt;Santiaago&lt;/small&gt;</description>
      <link>https://www.sanarias.com/blog/415BuildingtinygraphsanavatarwebserviceinGo/</link>
      <guid>https://www.sanarias.com/blog/415BuildingtinygraphsanavatarwebserviceinGo/</guid>
      <pubDate>Tue, 14 Apr 2015 06:27:10 -0000</pubDate>
    </item>
    
    <item>
      <title>Testing HTTP caching in Go</title>
      <description>&lt;h3&gt;&lt;a href=&#34;/blog/215TestingHTTPcachinginGo&#34; class=&#34;post-link&#34;&gt;Testing HTTP caching in Go&lt;/a&gt;&lt;/h3&gt;
&lt;h4&gt;&lt;small&gt;Feb 16, 2015&lt;/small&gt;&lt;/h4&gt;
&lt;p&gt;I have written some posts on &lt;a href=&#34;http://www.sanarias.com/blog/1214PlayingwithimagesinHTTPresponseingolang&#34;&gt;building images in an HTTP response&lt;/a&gt; and how to &lt;a href=&#34;http://www.sanarias.com/blog/115LearningHTTPcachinginGo&#34;&gt;cache them&lt;/a&gt;. This post is about how to test all of this and verify that you are testing the right thing.&lt;/p&gt;&lt;p&gt;This is the code that we are starting with:&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;func blackHandler(w http.ResponseWriter, r *http.Request) {&lt;br&gt;&lt;br&gt;    key := &#34;black&#34;&lt;br&gt;    e := `&#34;` + key + `&#34;`&lt;br&gt;    w.Header().Set(&#34;Etag&#34;, e)&lt;br&gt;    w.Header().Set(&#34;Cache-Control&#34;, &#34;max-age=2592000&#34;) // 30 days&lt;br&gt;&lt;br&gt;    if match := r.Header.Get(&#34;If-None-Match&#34;); match != &#34;&#34; {&lt;br&gt;        if strings.Contains(match, e) {&lt;br&gt;            w.WriteHeader(http.StatusNotModified)&lt;br&gt;            return&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    m := image.NewRGBA(image.Rect(0, 0, 240, 240))&lt;br&gt;    black := color.RGBA{0, 0, 0, 255}&lt;br&gt;    draw.Draw(m, m.Bounds(), &amp;image.Uniform{black}, image.ZP, draw.Src)&lt;br&gt;&lt;br&gt;    var img image.Image = m&lt;br&gt;    writeImage(w, &amp;img)&lt;br&gt;}&lt;/pre&gt;&lt;p&gt;&lt;code&gt;writeImage&lt;/code&gt; is a function that writes an image into a &lt;code&gt;http.ResponseWriter&lt;/code&gt; (&lt;i&gt;to read more about this go to my previous post &lt;a href=&#34;http://www.sanarias.com/blog/1214PlayingwithimagesinHTTPresponseingolang&#34;&gt;Playing with images in HTTP response in golang&lt;/a&gt;&lt;/i&gt;).&lt;/p&gt;&lt;h3 id=&#34;testing-the-handler&#34;&gt;Testing the handler:&lt;/h3&gt;&lt;p&gt;Lets start by making a first test to &lt;code&gt;blackHandler&lt;/code&gt;. This is done by using the &lt;a href=&#34;http://golang.org/pkg/net/http/httptest&#34;&gt;httptest package&lt;/a&gt;. We call the &lt;code&gt;NewRecorder()&lt;/code&gt; function which creates a &lt;a href=&#34;http://golang.org/pkg/net/http/httptest/#ResponseRecorder&#34;&gt;ResponseRecorder&lt;/a&gt; object.&lt;/p&gt;&lt;p&gt;This is great because you can use the &lt;strong&gt;recorder&lt;/strong&gt; as any &lt;strong&gt;http.ResponseWriter&lt;/strong&gt; and also use it to check the response that comes from the handler. In this case, we can check that the HTTP response code is equal to &lt;code&gt;http.StatusOK&lt;/code&gt;&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;package main&lt;br&gt;&lt;br&gt;import (&lt;br&gt;    &#34;net/http&#34;&lt;br&gt;    &#34;net/http/httptest&#34;&lt;br&gt;    &#34;testing&#34;&lt;br&gt;)&lt;br&gt;&lt;br&gt;func TestBlackHandler(t *testing.T) {&lt;br&gt;&lt;br&gt;    blackHandlerFunc := http.HandlerFunc(blackHandler)&lt;br&gt;    if r, err := http.NewRequest(&#34;GET&#34;, &#34;&#34;, nil); err != nil {&lt;br&gt;        t.Errorf(&#34;%v&#34;, err)&lt;br&gt;    } else {&lt;br&gt;        recorder := httptest.NewRecorder()&lt;br&gt;        blackHandlerFunc.ServeHTTP(recorder, r)&lt;br&gt;        if recorder.Code != http.StatusOK {&lt;br&gt;            t.Errorf(&#34;returned %v. Expected %v.&#34;, recorder.Code, http.StatusOK)&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;}&lt;/pre&gt;&lt;p&gt;Lets run our test.&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;    $ go test&lt;br&gt;    PASS&lt;br&gt;    ok  	local/cache	0.011s&lt;/pre&gt;&lt;p&gt;&lt;code&gt;local/cache&lt;/code&gt; is the path of my project in &lt;code&gt;$GOPATH/src/local/cache&lt;/code&gt;&lt;/p&gt;&lt;h3 id=&#34;test-coverage&#34;&gt;Test coverage:&lt;/h3&gt;&lt;p&gt;Now lets measure our test coverage for this test. &lt;b&gt;Go&lt;/b&gt; comes with a tool called &lt;b&gt;cover&lt;/b&gt; and a &lt;i&gt;great&lt;/i&gt; &lt;a href=&#34;http://blog.golang.org/cover&#34;&gt;article&lt;/a&gt; that explains all you need to know about it.&lt;/p&gt;&lt;p&gt;Lets start running the test coverage:&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;    $ go test -cover&lt;br&gt;    PASS&lt;br&gt;    coverage: 57.7% of statements&lt;br&gt;    ok  	local/cache	0.012s&lt;/pre&gt;&lt;p&gt;We know we are not covering the &lt;code&gt;main()&lt;/code&gt; function. This might explain why we have a lower percentage of coverage. Lets see what our test really covers:&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;    go test -coverprofile=coverage.out&lt;br&gt;    go tool cover -html=coverage.out&lt;/pre&gt;&lt;p&gt;This will open the following view in your browser:&lt;/p&gt;&lt;p&gt;&lt;img src=&#34;http://www.sanarias.com/static/img/cover1.png&#34; alt=&#34;coverage image of blackHandler&#34;&gt;&lt;/p&gt;&lt;p&gt;This is quite interesting, our test is covering most of the &lt;code&gt;blackHandler&lt;/code&gt; function, but not the code that deals with the &lt;b&gt;cache&lt;/b&gt;.&lt;/p&gt;&lt;p&gt;We need to improve our test so that the cache code is also covered.&lt;/p&gt;&lt;h3 id=&#34;testing-the-cache&#34;&gt;Testing the cache:&lt;/h3&gt;&lt;p&gt;To test the cache code we need to build a &lt;b&gt;second&lt;/b&gt; request with the &lt;strong&gt;Etag&lt;/strong&gt; of the first request (&lt;i&gt;If this is new to you, you might want to check &lt;a href=&#34;http://www.sanarias.com/blog/115LearningHTTPcachinginGo&#34;&gt;Learning HTTP caching in Go&lt;/a&gt;&lt;/i&gt;).&lt;/p&gt;&lt;p&gt;This can easily be done with the &lt;a href=&#34;http://golang.org/pkg/net/http/httptest/#ResponseRecorder&#34;&gt;ResponseRecorder&lt;/a&gt; type from the &lt;a href=&#34;http://golang.org/pkg/net/http/httptest/&#34;&gt;httptest&lt;/a&gt; package, since the &lt;b&gt;ResponseRecorder&lt;/b&gt; is an implementation of the &lt;a href=&#34;http://golang.org/pkg/net/http/#ResponseWriter&#34;&gt;http.ResponseWriter&lt;/a&gt;. So you can read information from it like you would do with a &lt;b&gt;http.ResponseWriter&lt;/b&gt; type.&lt;/p&gt;&lt;p&gt;You can read the Header for the &lt;b&gt;Etag&lt;/b&gt; value.&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;    // record etag to test cache&lt;br&gt;    etag = recorder.Header().Get(&#34;Etag&#34;)&lt;/pre&gt;&lt;p&gt;We then use the &lt;b&gt;Etag&lt;/b&gt; when building our &lt;b&gt;second&lt;/b&gt; request by setting it in the &lt;b&gt;If-None-Match&lt;/b&gt; in the &lt;code&gt;http.Header&lt;/code&gt;.&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;if r, err := http.NewRequest(&#34;GET&#34;, &#34;&#34;, nil); err != nil {&lt;br&gt;    t.Errorf(&#34;%v&#34;, err)&lt;br&gt;} else {&lt;br&gt;    r.Header.Set(&#34;If-None-Match&#34;, etag)&lt;br&gt;    ...&lt;br&gt;&lt;/pre&gt;&lt;p&gt;The last thing to do is to check this time that the &lt;code&gt;response.Code&lt;/code&gt; is equal to &lt;code&gt;http.StatusNotModified&lt;/code&gt;.&lt;/p&gt;&lt;p&gt;This is how our test looks now:&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;package main&lt;br&gt;&lt;br&gt;import (&lt;br&gt;    &#34;net/http&#34;&lt;br&gt;    &#34;net/http/httptest&#34;&lt;br&gt;    &#34;testing&#34;&lt;br&gt;)&lt;br&gt;&lt;br&gt;func TestBlackHandler(t *testing.T) {&lt;br&gt;&lt;br&gt;    blackHandlerFunc := http.HandlerFunc(blackHandler)&lt;br&gt;    var etag string&lt;br&gt;&lt;br&gt;    // first request&lt;br&gt;    if r, err := http.NewRequest(&#34;GET&#34;, &#34;&#34;, nil); err != nil {&lt;br&gt;        t.Errorf(&#34;%v&#34;, err)&lt;br&gt;    } else {&lt;br&gt;        recorder := httptest.NewRecorder()&lt;br&gt;        blackHandlerFunc.ServeHTTP(recorder, r)&lt;br&gt;        if recorder.Code != http.StatusOK {&lt;br&gt;            t.Errorf(&#34;returned %v. Expected %v.&#34;, recorder.Code, http.StatusOK)&lt;br&gt;        }&lt;br&gt;        // record etag to test cache&lt;br&gt;        etag = recorder.Header().Get(&#34;Etag&#34;)&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    // test caching&lt;br&gt;    if r, err := http.NewRequest(&#34;GET&#34;, &#34;&#34;, nil); err != nil {&lt;br&gt;        t.Errorf(&#34;%v&#34;, err)&lt;br&gt;    } else {&lt;br&gt;        r.Header.Set(&#34;If-None-Match&#34;, etag)&lt;br&gt;        recorder := httptest.NewRecorder()&lt;br&gt;        blackHandlerFunc.ServeHTTP(recorder, r)&lt;br&gt;        if recorder.Code != http.StatusNotModified {&lt;br&gt;            t.Errorf(&#34;returned %v. Expected %v.&#34;, recorder.Code, http.StatusNotModified)&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;}&lt;/pre&gt;&lt;p&gt;Let&#39;s see our coverage now:&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;    $ go test -cover&lt;br&gt;    PASS&lt;br&gt;    coverage: 69.2% of statements&lt;br&gt;    ok  	local/cache	0.010s&lt;/pre&gt;&lt;p&gt;That is better, lets check the coverage of the &lt;code&gt;blackHandler&lt;/code&gt; function:&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;    go test -coverprofile=coverage.out&lt;br&gt;    go tool cover -html=coverage.out&lt;/pre&gt;&lt;p&gt;&lt;img src=&#34;http://www.sanarias.com/static/img/cover2.png&#34; alt=&#34;coverage image&#34;&gt;&lt;/p&gt;&lt;p&gt;That looks nice! :) As I said before, the reason why we do not get 100% coverage is because there is no test covering &lt;code&gt;main()&lt;/code&gt; and error handling code in &lt;code&gt;writeImage()&lt;/code&gt;. But the important part was to properly test the &lt;code&gt;blackHander&lt;/code&gt; function.&lt;/p&gt;&lt;p&gt;The final example is available in this &lt;a href=&#34;https://gist.github.com/santiaago/afc76ea1df5b89d71138&#34;&gt;gist&lt;/a&gt;&lt;/p&gt;&lt;p&gt;I hope you found this post useful and that this will help you build proper tests for your applications.&lt;/p&gt;&lt;p&gt;Here are some resources that helped me come up with this article:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&#34;http://blog.golang.org/cover&#34;&gt;The cover story&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://markjberger.com/testing-web-apps-in-golang/&#34;&gt;Testing Web Apps in Go&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://golang.org/pkg/net/http/httptest/&#34;&gt;httptest package&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;Follow me at &lt;a href=&#34;https://twitter.com/santiago_arias&#34;&gt;@santiago_arias&lt;/a&gt; to be notified about more posts like this.&lt;/p&gt;&lt;small class=&#34;pull-right&#34;&gt;Santiaago&lt;/small&gt;</description>
      <link>https://www.sanarias.com/blog/215TestingHTTPcachinginGo/</link>
      <guid>https://www.sanarias.com/blog/215TestingHTTPcachinginGo/</guid>
      <pubDate>Mon, 16 Feb 2015 13:05:42 -0000</pubDate>
    </item>
    
    <item>
      <title>Learning HTTP caching in Go</title>
      <description>&lt;h3&gt;&lt;a href=&#34;/blog/115LearningHTTPcachinginGo&#34; class=&#34;post-link&#34;&gt;Learning HTTP caching in Go&lt;/a&gt;&lt;/h3&gt;
&lt;h4&gt;&lt;small&gt;Jan 19, 2015&lt;/small&gt;&lt;/h4&gt;
&lt;p&gt;When you are building a web app, you want to make sure that you are caching the app&#39;s resources. So if for example a specific URL always delivers the same image, you want to cache it in the browser to avoid unnecessary traffic and have a better  web performance.&lt;/p&gt;&lt;p&gt;For the rest of this post I suppose you have the following web application. It generates an image when the &lt;code&gt;/black/&lt;/code&gt; URL is called. I would like to cache that image.&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;package main&lt;br&gt;    &lt;br&gt;import (&lt;br&gt;    &#34;bytes&#34;&lt;br&gt;    &#34;flag&#34;&lt;br&gt;    &#34;image&#34;&lt;br&gt;    &#34;image/color&#34;&lt;br&gt;    &#34;image/draw&#34;&lt;br&gt;    &#34;image/jpeg&#34;&lt;br&gt;    &#34;log&#34;&lt;br&gt;    &#34;net/http&#34;&lt;br&gt;    &#34;strconv&#34;&lt;br&gt;)&lt;br&gt;    &lt;br&gt;var root = flag.String(&#34;root&#34;, &#34;.&#34;, &#34;file system path&#34;)&lt;br&gt;&lt;br&gt;func main() {&lt;br&gt;    http.HandleFunc(&#34;/black/&#34;, blackHandler)&lt;br&gt;    http.Handle(&#34;/&#34;, http.FileServer(http.Dir(*root)))&lt;br&gt;    log.Println(&#34;Listening on 8080&#34;)&lt;br&gt;    err := http.ListenAndServe(&#34;:8080&#34;, nil)&lt;br&gt;    if err != nil {&lt;br&gt;        log.Fatal(&#34;ListenAndServe:&#34;, err)&lt;br&gt;    }&lt;br&gt;}&lt;br&gt;&lt;br&gt;func blackHandler(w http.ResponseWriter, r *http.Request) {&lt;br&gt;    m := image.NewRGBA(image.Rect(0, 0, 240, 240))&lt;br&gt;    black := color.RGBA{0, 0, 0, 255}&lt;br&gt;    draw.Draw(m, m.Bounds(), &amp;image.Uniform{black}, image.ZP, draw.Src)&lt;br&gt;    &lt;br&gt;    var img image.Image = m&lt;br&gt;    writeImage(w, &amp;img)&lt;br&gt;}&lt;br&gt;    &lt;br&gt;// writeImage encodes an image in jpeg format and writes it into ResponseWriter.&lt;br&gt;func writeImage(w http.ResponseWriter, img *image.Image) {&lt;br&gt;    &lt;br&gt;    buffer := new(bytes.Buffer)&lt;br&gt;    if err := jpeg.Encode(buffer, *img, nil); err != nil {&lt;br&gt;        log.Println(&#34;unable to encode image.&#34;)&lt;br&gt;    }&lt;br&gt;    &lt;br&gt;    w.Header().Set(&#34;Content-Type&#34;, &#34;image/jpeg&#34;)&lt;br&gt;    w.Header().Set(&#34;Content-Length&#34;, strconv.Itoa(len(buffer.Bytes())))&lt;br&gt;    if _, err := w.Write(buffer.Bytes()); err != nil {&lt;br&gt;        log.Println(&#34;unable to write image.&#34;)&lt;br&gt;    }&lt;br&gt;}&lt;/pre&gt;&lt;h3 id=&#34;how-to-cache-&#34;&gt;How to cache?&lt;/h3&gt;&lt;p&gt;This subject is new to me, I didn&#39;t know you could set some caching information in the HTTP headers (&lt;i&gt;there is so much to learn :-)&lt;/i&gt; ).&lt;/p&gt;&lt;p&gt;This is what the &lt;b&gt;HTTP header&lt;/b&gt; of &lt;code&gt;/black/&lt;/code&gt; looks like right now:&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;santiaago$ curl -I localhost:8080/black/&lt;br&gt;HTTP/1.1 200 OK&lt;br&gt;Content-Length: 1493&lt;br&gt;Content-Type: image/jpeg&lt;br&gt;Date: Sun, 18 Jan 2015 14:17:37 GMT&lt;/pre&gt;&lt;p&gt;There are two things that can be done in order to cache this HTTP request:&lt;/p&gt;&lt;h4 id=&#34;setting-an-etag-&#34;&gt;Setting an etag:&lt;/h4&gt;&lt;p&gt;An &lt;a href=&#34;http://en.wikipedia.org/wiki/HTTP_ETag&#34;&gt;etag&lt;/a&gt; or &lt;b&gt;entity tag&lt;/b&gt; is a mechanisms that HTTP provides to deal with caching. If you ask multiple times for the same resource you get the resource for &lt;i&gt;free&lt;/i&gt; once it is cached.&lt;/p&gt;&lt;p&gt;The etag is what will identify a specific resource so it should be a unique key in your web app.&lt;/p&gt;&lt;p&gt;In Go this is how you could set the &lt;b&gt;etag&lt;/b&gt; in the HTTP header:&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;func Handler(w http.ResponseWriter, r *http.Request) {&lt;br&gt;    key := &#34;somekey&#34;&lt;br&gt;    e := `&#34;` + key + `&#34;`&lt;br&gt;    w.Header().Set(&#34;Etag&#34;, e)&lt;br&gt;    ...&lt;/pre&gt;&lt;h4 id=&#34;reading-the-http-request-&#34;&gt;Reading the HTTP request:&lt;/h4&gt;&lt;p&gt;Suppose you send the HTTP response to the client and he asks for the same resource again. In this case you need to read the HTTP header of the &lt;b&gt;HTTP.request&lt;/b&gt; and check the &lt;code&gt;If-None-Match&lt;/code&gt; field. This field will have the &lt;b&gt;etag&lt;/b&gt; key value if the client has already asked for that resource.&lt;/p&gt;&lt;p&gt;If there is a &lt;b&gt;match&lt;/b&gt; between the &lt;code&gt;If-None-Match&lt;/code&gt; field and the key you &lt;b&gt;generate&lt;/b&gt; in the server, there is no need to rebuild the image again as the browser already has it. In that case set the HTTP status to &lt;code&gt;StatusNotModified&lt;/code&gt; ie &lt;code&gt;304&lt;/code&gt; and return.&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;if match := r.Header.Get(&#34;If-None-Match&#34;); match != &#34;&#34; {&lt;br&gt;    if strings.Contains(match, e) {&lt;br&gt;        w.WriteHeader(http.StatusNotModified)&lt;br&gt;        return&lt;br&gt;    }&lt;br&gt;}&lt;/pre&gt;&lt;h4 id=&#34;setting-cache-control-&#34;&gt;Setting cache-control:&lt;/h4&gt;&lt;br&gt;&lt;p&gt;Setting &lt;code&gt;cache-control&lt;/code&gt; to a specific date tells the client that once the date expires the cache should revalidate the resource. You can also set the &lt;code&gt;cache-control&lt;/code&gt; value to other values. This resource is great to understand cache-control: &lt;a href=&#34;http://www.mobify.com/blog/beginners-guide-to-http-cache-headers/&#34;&gt;A Beginner&#39;s Guide to HTTP Cache Headers&lt;/a&gt;&lt;/p&gt;&lt;p&gt;This is how you could set your cache in Go:&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;w.Header().Set(&#34;Cache-Control&#34;, &#34;max-age=2592000&#34;) // 30 days&lt;/pre&gt;&lt;h4 id=&#34;all-together-&#34;&gt;All together:&lt;/h4&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;func blackHandler(w http.ResponseWriter, r *http.Request) {&lt;br&gt;    &lt;br&gt;    key := &#34;black&#34;&lt;br&gt;    e := `&#34;` + key + `&#34;`&lt;br&gt;    w.Header().Set(&#34;Etag&#34;, e)&lt;br&gt;    w.Header().Set(&#34;Cache-Control&#34;, &#34;max-age=2592000&#34;) // 30 days&lt;br&gt;    &lt;br&gt;    if match := r.Header.Get(&#34;If-None-Match&#34;); match != &#34;&#34; {&lt;br&gt;        if strings.Contains(match, e) {&lt;br&gt;            w.WriteHeader(http.StatusNotModified)&lt;br&gt;            return&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;    &lt;br&gt;    m := image.NewRGBA(image.Rect(0, 0, 240, 240))&lt;br&gt;    black := color.RGBA{0, 0, 0, 255}&lt;br&gt;    draw.Draw(m, m.Bounds(), &amp;image.Uniform{black}, image.ZP, draw.Src)&lt;br&gt;    &lt;br&gt;    var img image.Image = m&lt;br&gt;    writeImage(w, &amp;img)&lt;br&gt;}&lt;/pre&gt;&lt;h3 id=&#34;testing-&#34;&gt;Testing:&lt;/h3&gt;&lt;p&gt;Once your handler is ready to cache, you want to test that everything is in place.&lt;/p&gt;&lt;p&gt;The first thing you can do is take a look at the &lt;b&gt;Network&lt;/b&gt; section in &lt;a href=&#34;https://developer.chrome.com/devtools&#34;&gt;Chrome Developer tools&lt;/a&gt; or any dev tools you have.&lt;/p&gt;&lt;p&gt;Ask for your resource, in this case &lt;code&gt;localhost:8080/black/&lt;/code&gt;. You should get a Status &lt;code&gt;200&lt;/code&gt; &lt;code&gt;OK&lt;/code&gt;, then hit a refresh and check that the Status has now changed to &lt;code&gt;304&lt;/code&gt; &lt;code&gt;StatusNotModified&lt;/code&gt;. You can also notice that the latency of the second request is shorted than the first one.&lt;/p&gt;&lt;br&gt;&lt;p&gt;&lt;img class=&#34;img-polaroid&#34; src=&#34;http://www.sanarias.com/static/img/cache.png&#34; alt=&#34;network of /black request&#34;&gt;&lt;/p&gt;&lt;br&gt;&lt;p&gt;If you take a look at the HTTP header of the response you should be able to check the &lt;strong&gt;Etag&lt;/strong&gt; and &lt;b&gt;Cache-Control&lt;/b&gt; values.&lt;/p&gt;&lt;p&gt;You can also do this with &lt;a href=&#34;http://curl.haxx.se/&#34;&gt;curl&lt;/a&gt; by running the following command:&lt;/p&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;santiaago$ curl -I localhost:8080/black/&lt;br&gt;HTTP/1.1 200 OK&lt;br&gt;Cache-Control: max-age=2592000&lt;br&gt;Content-Length: 1493&lt;br&gt;Content-Type: image/jpeg&lt;br&gt;Etag: &#34;black&#34;&lt;br&gt;Date: Sun, 18 Jan 2015 14:41:31 GMT&lt;/pre&gt;&lt;p&gt;The final example is available in this &lt;a href=&#34;https://gist.github.com/santiaago/9dd60f0403e80bcec71d&#34;&gt;gist&lt;/a&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;Some links that helped understand this:&lt;/b&gt;&lt;/p&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&#34;http://en.wikipedia.org/wiki/HTTP_ETag&#34;&gt;Etag wiki&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://stackoverflow.com/questions/998791/if-modified-since-vs-if-none-match&#34;&gt;SO if-midified-since vs if-none-match&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://www.mobify.com/blog/beginners-guide-to-http-cache-headers/&#34;&gt;A Beginner&#39;s Guide to HTTP Cache Headers&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://stackoverflow.com/questions/6486805/html-cache-control-max-age&#34;&gt;SO html cache control max age&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://odino.org/don-t-rape-http-if-none-match-the-412-http-status-code/&#34;&gt;Don&#39;t rape HTTP: If-(None-)Match &amp;amp; the 412 HTTP status code&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://www.w3.org/Protocols/rfc2616/rfc2616-sec13.html&#34;&gt;Caching in HTTP&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching?hl=en&#34;&gt;HTTP caching - Google Web Fundamentals&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://golang.org/pkg/net/http/#Header&#34;&gt;Go HTTP package&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;Follow me at &lt;a href=&#34;https://twitter.com/santiago_arias&#34;&gt;@santiago_arias&lt;/a&gt; to be notified about more posts like this.&lt;/p&gt;&lt;small class=&#34;pull-right&#34;&gt;Santiaago&lt;/small&gt;</description>
      <link>https://www.sanarias.com/blog/115LearningHTTPcachinginGo/</link>
      <guid>https://www.sanarias.com/blog/115LearningHTTPcachinginGo/</guid>
      <pubDate>Mon, 19 Jan 2015 11:57:31 -0000</pubDate>
    </item>
    
    <item>
      <title>Building a chessboard image in Go</title>
      <description>&lt;h3&gt;&lt;a href=&#34;/blog/115BuildingachessboardimageinGo&#34; class=&#34;post-link&#34;&gt;Building a chessboard image in Go&lt;/a&gt;&lt;/h3&gt;
&lt;h4&gt;&lt;small&gt;Jan 12, 2015&lt;/small&gt;&lt;/h4&gt;
&lt;p&gt;I have been building images in Go and I would like to share how to build a chessboard image pattern with some &lt;a href=&#34;http://en.wikipedia.org/wiki/Modulo_operation#Modulo_operation_expression&#34;&gt;modulo operations&lt;/a&gt;. In my case I am building a &lt;code&gt;6x6&lt;/code&gt; grid of size &lt;code&gt;240px&lt;/code&gt;. That means each square is a &lt;code&gt;40px&lt;/code&gt; square.&lt;/p&gt;&lt;p&gt;I would like to build an image where one square (40x40) is black the next one white and so on. Also the colors alternate between each row.&lt;br&gt;&lt;br&gt;&lt;img src=&#34;http://sanarias.com/static/img/chessboard.jpg&#34; alt=&#34;chessboard&#34; class=&#34;img-polaroid&#34;&gt;&lt;br&gt;&lt;/p&gt;&lt;h4 id=&#34;looping-through-the-image-&#34;&gt;Looping through the image:&lt;/h4&gt;&lt;p&gt;To be able to make this chessboard we first need to loop through the &lt;b&gt;X&lt;/b&gt;&#39;s and &lt;b&gt;Y&lt;/b&gt;&#39;s of the image. Here is how you would create a black image by coloring each &#34;pixel&#34; in black.&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;func drawBlack(m *image.RGBA, color1 color.RGBA) {&lt;br&gt;    size := m.Bounds().Size()&lt;br&gt;    for x := 0; x &lt; size.X; x++ {&lt;br&gt;        for y := 0; y &lt; size.Y; y++ {&lt;br&gt;            m.Set(x, y, color1)&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;}&lt;/pre&gt;&lt;/p&gt;&lt;h4 id=&#34;saving-the-image-&#34;&gt;Saving the image:&lt;/h4&gt;&lt;p&gt;This is how you could save the image you built:&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;func main() {&lt;br&gt;    m := image.NewRGBA(image.Rect(0, 0, 240, 240))&lt;br&gt;    black := color.RGBA{uint8(0), uint8(0), 0, 255}&lt;br&gt;    &lt;br&gt;    drawBlack(m, black)&lt;br&gt;    &lt;br&gt;    if img, err := os.Create(&#34;black.jpg&#34;); err != nil {&lt;br&gt;        log.Println(&#34;unable to create black.jpg: %v&#34;, err)&lt;br&gt;    } else {&lt;br&gt;        jpeg.Encode(img, m, nil)&lt;br&gt;        defer img.Close()&lt;br&gt;    }&lt;br&gt;}&lt;/pre&gt;&lt;/p&gt;&lt;h4 id=&#34;modulo-of-&#34;&gt;modulo of ...:&lt;/h4&gt;&lt;p&gt;The first thing that came to mind my mind was that I should use &lt;b&gt;modulo&lt;/b&gt; of something..&lt;br&gt;We have a 2 level loop through &lt;b&gt;X&lt;/b&gt;&#39;s and &lt;b&gt;Y&lt;/b&gt;&#39;s. Let&#39;s try to build an image that draws black if &lt;code&gt;x%2 == 0&lt;/code&gt; and draws white otherwise.&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;func drawModuloOnX(m *image.RGBA, color1, color2 color.RGBA) {&lt;br&gt;    size := m.Bounds().Size()&lt;br&gt;    for x := 0; x &lt; size.X; x++ {&lt;br&gt;        for y := 0; y &lt; size.Y; y++ {&lt;br&gt;            if x%2 == 0 {&lt;br&gt;                m.Set(x, y, color1)&lt;br&gt;            } else {&lt;br&gt;                m.Set(x, y, color2)&lt;br&gt;            }&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;}&lt;/pre&gt;This is the image that we get:&lt;br&gt;&lt;br&gt;&lt;img src=&#34;http://sanarias.com/static/img/mod2x.jpg&#34; alt=&#34;mod2x&#34; class=&#34;img-polaroid&#34;&gt;&lt;br&gt;&lt;br&gt;If we do instead &lt;code&gt;y%2 == 0&lt;/code&gt; we get the following:&lt;br&gt;&lt;br&gt;&lt;img src=&#34;http://sanarias.com/static/img/mod2y.jpg&#34; alt=&#34;mod2y&#34; class=&#34;img-polaroid&#34;&gt;&lt;br&gt;&lt;br&gt;How about &lt;code&gt;(x+y)%2 == 0&lt;/code&gt;?&lt;br&gt;&lt;br&gt;&lt;img src=&#34;http://sanarias.com/static/img/mod2xplusy.jpg&#34; alt=&#34;mod2xplusy&#34; class=&#34;img-polaroid&#34;&gt;&lt;br&gt;&lt;br&gt;Well, we are getting closer, but this is making a chessboard grid of &lt;b&gt;240x240&lt;/b&gt; and what we want is a &lt;b&gt;6x6&lt;/b&gt; chessboard grid. That means, to color a specific &lt;b&gt;range/square&lt;/b&gt; with black and another &lt;b&gt;range/square&lt;/b&gt; in white.&lt;/p&gt;&lt;h4 id=&#34;indexes-and-ranges-&#34;&gt;Indexes and ranges:&lt;/h4&gt;&lt;p&gt;In order to color the image by a specific range let&#39;s take first a look at the first row of the image:&lt;br&gt;&lt;br&gt;This is the index of that first row:&lt;br&gt;&lt;pre style=&#34;font-size:9px&#34; class=&#34;prettyprint&#34;&gt;0 1 2 ... 38 39 40 41 ... 78 79 80 81 ... 118 119 120 121 ... 158 159 160 161 ... 198 199 200 201 ... 238 239&lt;/pre&gt;If we can get an array of the first row of the &lt;b&gt;6x6&lt;/b&gt; grid to have &lt;b&gt;0&lt;/b&gt; if the color is black and &lt;b&gt;1&lt;/b&gt; if it is white, we are in good shape. We would like to build an array that transforms the indexes of this first row in the following:&lt;br&gt;&lt;pre  style=&#34;font-size:9px&#34; class=&#34;prettyprint&#34;&gt;indexes:  0 1 2 ... 38 39 40 41 ... 78 79 80 81 ... 118 119 120 121 ... 158 159 160 161 ... 198 199 200 201 ... 238 239&lt;br&gt;transform:0 0 0 ... 0  0  1  1  ... 1  1  0  0  ... 0   0   1   1   ... 1   1   0   0       0   0   1   1   ... 1   1&lt;/pre&gt;To do this we need to first divide the indexes by the size of a &lt;b&gt;square&lt;/b&gt; in this case &lt;b&gt;40&lt;/b&gt;. We get the following pattern:&lt;br&gt;&lt;pre style=&#34;font-size:9px&#34; class=&#34;prettyprint&#34;&gt;indexes:  0 1 2 ... 38 39 40 41 ... 78 79 80 81 ... 118 119 120 121 ... 158 159 160 161 ... 198 199 200 201 ... 238 239&lt;br&gt; /40:     0 0 0 ... 0  0  1  1  ... 1  1  2  2  ... 2   2   3   3   ... 3   3   4   4   ... 4   4   5   5   ... 5   5&lt;/pre&gt;This is close to what we want. If we use the &lt;b&gt;modulo of 2&lt;/b&gt; now on the result of the previous array we get:&lt;br&gt;&lt;pre style=&#34;font-size:9px&#34; class=&#34;prettyprint&#34;&gt;indexes:  0 1 2 ... 38 39 40 41 ... 78 79 80 81 ... 118 119 120 121 ... 158 159 160 161 ... 198 199 200 201 ... 238 239&lt;br&gt;transform:0 0 0 ... 0  0  1  1  ... 1  1  0  0  ... 0   0   1   1   ... 1   1   0   0       0   0   1   1   ... 1   1&lt;/pre&gt;&lt;p&gt;This looks good, let&#39;s build the image!&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;func draw(m *image.RGBA, color1, color2 color.RGBA) {&lt;br&gt;    size := m.Bounds().Size()&lt;br&gt;    quadrant := size.X / 6&lt;br&gt;    for x := 0; x &lt; size.X; x++ {&lt;br&gt;        val := (x / quadrant) % 2&lt;br&gt;        for y := 0; y &lt; size.Y; y++ {&lt;br&gt;            if val == 0 {&lt;br&gt;                m.Set(x, y, color1)&lt;br&gt;            } else {&lt;br&gt;                m.Set(x, y, color2)&lt;br&gt;            }&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;}&lt;/pre&gt;Notice that in our case &lt;code&gt;size = 240&lt;/code&gt; and&lt;code&gt;quadrant = 40&lt;/code&gt;.&lt;br&gt;Now if we create our image using the above algorithm we get the following:&lt;br&gt;&lt;br&gt;&lt;img src=&#34;http://sanarias.com/static/img/vertical.jpg&#34; alt=&#34;vertical&#34; class=&#34;img-polaroid&#34;&gt;&lt;br&gt;&lt;br&gt;Similarly if we now do &lt;code&gt;val = (y / quadrant) %2&lt;/code&gt; we get the following:&lt;br&gt;&lt;br&gt;&lt;img src=&#34;http://sanarias.com/static/img/horizontal.jpg&#34; alt=&#34;horizontal&#34; class=&#34;img-polaroid&#34;&gt;&lt;br&gt;&lt;/p&gt;&lt;h4 id=&#34;combining-values-&#34;&gt;Combining values:&lt;/h4&gt;&lt;p&gt;As in section &lt;b&gt;&#34;modulo of ...&#34;&lt;/b&gt;, if we combine the &lt;b&gt;vertical&lt;/b&gt; and &lt;b&gt;horizontal&lt;/b&gt; values and compute a modulo:&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;func drawGrid(m *image.RGBA, color1, color2 color.RGBA) {&lt;br&gt;    size := m.Bounds().Size()&lt;br&gt;    quad := size.X / 6&lt;br&gt;    for x := 0; x &lt; size.X; x++ {&lt;br&gt;        val := (x / quad) % 2&lt;br&gt;        for y := 0; y &lt; size.Y; y++ {&lt;br&gt;            val2 := (y / quad) % 2&lt;br&gt;            if (val+val2)%2 == 0 {&lt;br&gt;                m.Set(x, y, color1)&lt;br&gt;            } else {&lt;br&gt;                m.Set(x, y, color2)&lt;br&gt;            }&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;}&lt;/pre&gt;We finally get the following \o/:&lt;br&gt;&lt;br&gt;&lt;div class=&#34;text-center&#34;&gt;&lt;img src=&#34;http://sanarias.com/static/img/chessboard.jpg&#34; alt=&#34;chessboard&#34; class=&#34;img-polaroid&#34;&gt;&lt;/div&gt;&lt;br&gt;&lt;br&gt;I hope you found this interesting. Here is the final example also available in this &lt;a href=&#34;https://gist.github.com/santiaago/a749e510cd95d50314db&#34;&gt;gist&lt;/a&gt;&lt;br&gt;&lt;br&gt;&lt;script src=&#34;https://gist.github.com/santiaago/a749e510cd95d50314db.js&#34;&gt;&lt;/script&gt;&lt;br&gt;&lt;b&gt;Some links that helped me making this:&lt;/b&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&#34;http://golang.org/pkg/image/&#34;&gt;package image&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://golang.org/pkg/image/jpeg/&#34;&gt;package image/jpeg&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://golang.org/pkg/os&#34;&gt;package os&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://blog.golang.org/defer-panic-and-recover&#34;&gt;Defer, Panic, and Recover&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;https://golang.org/doc/effective_go.html&#34;&gt;Effective Go&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;Follow me at &lt;a href=&#34;https://twitter.com/santiago_arias&#34;&gt;@santiago_arias&lt;/a&gt; to be notified about more posts like this.&lt;/p&gt;&lt;small class=&#34;pull-right&#34;&gt;Santiaago&lt;/small&gt;</description>
      <link>https://www.sanarias.com/blog/115BuildingachessboardimageinGo/</link>
      <guid>https://www.sanarias.com/blog/115BuildingachessboardimageinGo/</guid>
      <pubDate>Mon, 12 Jan 2015 22:06:56 -0000</pubDate>
    </item>
    
    <item>
      <title>2014 in review</title>
      <description>&lt;h3&gt;&lt;a href=&#34;/blog/12142014inreview&#34; class=&#34;post-link&#34;&gt;2014 in review&lt;/a&gt;&lt;/h3&gt;
&lt;h4&gt;&lt;small&gt;Dec 30, 2014&lt;/small&gt;&lt;/h4&gt;
&lt;p&gt;As in previous years, I would like to make a review of the year 2014. A lot of things happened: new job, new home, new city, new country. Doing all of that was not an easy task and takes a lot of time. Yet, I was able to achieve multiple goals  and continue learning :-).&lt;br&gt;Here is my github contributions this year. You can see that big gap between July and October. This was the time when all the changes happened, interviews, new job, relocation, ...&lt;br&gt;&lt;img src=&#34;http://www.sanarias.com/static/img/git-sas-2014.png&#34;  alt=&#34;git contributions in 2014&#34;/&gt;&lt;/p&gt;&lt;br&gt;&lt;h4&gt;gonawin:&lt;/h4&gt;&lt;p&gt;The main project of this year was &lt;a href=&#34;http://www.gonawin.com&#34;&gt;gonawin&lt;/a&gt;, a social web application which I did with &lt;a href=&#34;http://www.twitter.com/rjourde&#34;&gt;Remy Jourde&lt;/a&gt; and &lt;a href=&#34;https://plus.google.com/+CarmenRebolledo&#34;&gt;Carmen&lt;/a&gt; to make &lt;i&gt;friendly bets&lt;/i&gt; on sport games with your friends. The repository is on github with the name of &lt;a href=&#34;http://www.github.com/taironas/gonawin&#34;&gt;gonawin&lt;/a&gt;.&lt;br&gt;It was built in Go, Google App Engine and AngularJS. We got a total of &lt;b&gt;81&lt;/b&gt; users and about &lt;b&gt;50&lt;/b&gt; active users for the world cup. We are planning to continue working on this project and keep improving it step by step.&lt;/p&gt;&lt;h4&gt;Moocs:&lt;/h4&gt;&lt;p&gt;The only online course that I made this year was &lt;a href=&#34;http://work.caltech.edu/telecourse.html&#34;&gt;Learning from data&lt;/a&gt;. I had already done this course in python &lt;a href=&#34;https://github.com/santiaago/caltech.ml&#34;&gt;here&lt;/a&gt;, but I decided to do it again in &lt;a href=&#34;http://www.golang.org&#34;&gt;Go&lt;/a&gt;. I haven&#39;t finished it yet, right now I am doing the implementation of &lt;i&gt;Support Vector Machines&lt;/i&gt;. There are two reasons on why I am doing this course again. The first one is to continue learning Go. The second reason is that I want to build my own &lt;i&gt;Machine learning&lt;/i&gt; framework in Go in order to use it in future projects that I have in mind. One of them is a Machine Learning Visualizer built in Go and &lt;b&gt;d3.js&lt;/b&gt;. The github repository for this online course is available: &lt;a href=&#34;https://github.com/santiaago/caltechx.go&#34;&gt;here&lt;/a&gt;&lt;/p&gt;&lt;h4&gt;Continue learning Go:&lt;/h4&gt;&lt;p&gt;I set this as one of my goals for &lt;a href=&#34;http://www.sanarias.com/blog/4142013inreview&#34;&gt;2013&lt;/a&gt;, and I continue to progress in this topic. I did several projects in order to learn more about this language.&lt;br&gt;The main one was &lt;a href=&#34;http://www.gonawin.com&#34;&gt;gonawin&lt;/a&gt;, a web application whose backend is written in Go.&lt;br&gt;After we finished this app, we decided to start refactoring some parts of the code and improving the app itself. So we created a github organization called &lt;a href=&#34;http://www.github.com/taironas&#34;&gt;taironas&lt;/a&gt; and we are currently working on two  projects:&lt;ul&gt;&lt;li&gt;&lt;a href=&#34;http://www.github.com/taironas/route&#34;&gt;route&lt;/a&gt;, a URL router that allows regex expression in URL paths.&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://github.com/taironas/tinygraphs&#34;&gt;tinygraphs&lt;/a&gt;, an avatar generator web service.&lt;/li&gt;&lt;/ul&gt;I also attended a &lt;a href=&#34;http://www.dotgo.eu/&#34;&gt;DotGo&lt;/a&gt; conference which was really fun. And I blogged about some Go things I have discovered while coding:&lt;ul&gt;&lt;li&gt;&lt;a href=&#34;http://www.sanarias.com/blog/214OrderdateschronologicallyinGo&#34;&gt;Order dates chronologically in Go&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://www.sanarias.com/blog/1214PlayingwithimagesinHTTPresponseingolang&#34;&gt;Playing with images in HTTP response in golang&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/p&gt;&lt;h4&gt;More learning:&lt;/h4&gt;&lt;p&gt;Books I have read and worked on:&lt;ul&gt;&lt;li&gt;&lt;a href=&#34;http://www.amazon.co.uk/Algorithm-Design-Manual-Steven-Skiena/dp/1848000693&#34;&gt;The algorithm design manual&lt;/a&gt;&lt;ul&gt;&lt;li&gt;Mainly some reading through the first 6 chapters and I started coding the exercises of the first 2 chapters in this &lt;a href=&#34;http://www.github.com/santiaago/algorithm-design-manual&#34;&gt;repo&lt;/a&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://www.amazon.co.uk/Cracking-Coding-Interview-Programming-Questions/dp/098478280X&#34;&gt;Cracking the coding interview&lt;/a&gt;&lt;ul&gt;&lt;li&gt;This is a great book if you want to get in shape and ready for some interviews.&lt;/li&gt;&lt;/ul&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/p&gt;&lt;h4&gt;Project wish list for 2015:&lt;/h4&gt;&lt;p&gt;Here is what I wish to accomplish this year:&lt;ul&gt;&lt;li&gt;Finish main work on the &lt;a href=&#34;http://github.com/taironas/route&#34;&gt;route&lt;/a&gt; package.&lt;/li&gt;&lt;li&gt;Finish first version of the web service avatar generator &lt;a href=&#34;http://www.github.com/taironas/tinygraphs&#34;&gt;tinygraphs&lt;/a&gt;.&lt;/li&gt;&lt;li&gt;Enhance &lt;a href=&#34;http://www.github.com/taironas/gonawin&#34;&gt;gonawin&lt;/a&gt;.&lt;/li&gt;&lt;li&gt;Finish &lt;a href=&#34;http://www.github.com/santiaago/caltechx.go&#34;&gt;caltechx.go&lt;/a&gt;&lt;/li&gt;&lt;li&gt;Extract the machine learning code from &lt;b&gt;caltechx.go&lt;/b&gt; into a separate repo.&lt;/li&gt;&lt;li&gt;Make a web site to visualize machine learning models and methods using &lt;b&gt;d3.js&lt;/b&gt;.&lt;/li&gt;&lt;li&gt;Any interesting unplanned project :-)&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;Let&#39;s see what 2015 brings. :-)&lt;/p&gt;&lt;small class=&#34;pull-right&#34;&gt;Santiaago&lt;/small&gt;</description>
      <link>https://www.sanarias.com/blog/12142014inreview/</link>
      <guid>https://www.sanarias.com/blog/12142014inreview/</guid>
      <pubDate>Tue, 30 Dec 2014 18:37:05 -0000</pubDate>
    </item>
    
    <item>
      <title>Playing with images in HTTP response in golang</title>
      <description>&lt;h3&gt;&lt;a href=&#34;/blog/1214PlayingwithimagesinHTTPresponseingolang&#34; class=&#34;post-link&#34;&gt;Playing with images in HTTP response in golang&lt;/a&gt;&lt;/h3&gt;
&lt;h4&gt;&lt;small&gt;Dec 26, 2014&lt;/small&gt;&lt;/h4&gt;
&lt;p&gt;I am currently building an avatar generator with &lt;a href=&#34;https://twitter.com/rjourde&#34;&gt;rjourde&lt;/a&gt; in &lt;a href=&#34;http://www.golang.org&#34;&gt;Go&lt;/a&gt;.&lt;br&gt;    In the process of doing it I am learning about how to create images on the fly and display them in an html page.&lt;br&gt;Here is what I found today:&lt;/p&gt;&lt;h4&gt;Objective&lt;/h4&gt;&lt;p&gt;I want to have a simple web server, with two entry points: &lt;code&gt;/blue&lt;/code&gt; and &lt;code&gt;/red&lt;/code&gt;.&lt;br&gt;This two entry points will display a blue or red image that will be created on the fly.&lt;br&gt;        &lt;pre class=&#34;prettyprint&#34;&gt;    var root = flag.String(&#34;root&#34;, &#34;.&#34;, &#34;file system path&#34;)&lt;br&gt;&lt;br&gt;    func main() {&lt;br&gt;        http.HandleFunc(&#34;/blue/&#34;, blueHandler)&lt;br&gt;        http.HandleFunc(&#34;/red/&#34;, redHandler)&lt;br&gt;        http.Handle(&#34;/&#34;, http.FileServer(http.Dir(*root)))&lt;br&gt;        log.Println(&#34;Listening on 8080&#34;)&lt;br&gt;        err := http.ListenAndServe(&#34;:8080&#34;, nil)&lt;br&gt;        if err != nil {&lt;br&gt;            log.Fatal(&#34;ListenAndServe:&#34;, err)&lt;br&gt;        }&lt;br&gt;    }&lt;/pre&gt;&lt;h4&gt;Building an image:&lt;/h4&gt;&lt;p&gt;For this specific example there is no much complexity in the image I want to create. So a simple &lt;a href=&#34;http://en.wikipedia.org/wiki/RGBA_color_space&#34;&gt;RGBA&lt;/a&gt; image will do the work. We do this with three golang packages: &lt;a href=&#34;http://golang.org/pkg/image/&#34;&gt;image&lt;/a&gt;,  &lt;a href=&#34;http://golang.org/pkg/image/color&#34;&gt;image/color&lt;/a&gt;, &lt;a href=&#34;http://golang.org/pkg/image/draw&#34;&gt;image/draw&lt;/a&gt;.&lt;br&gt;            &lt;pre class=&#34;prettyprint&#34;&gt;    m := image.NewRGBA(image.Rect(0, 0, 240, 240))&lt;br&gt;    blue := color.RGBA{0, 0, 255, 255}&lt;br&gt;    draw.Draw(m, m.Bounds(), &amp;image.Uniform{blue}, image.ZP, draw.Src)&lt;/pre&gt;&lt;/p&gt;&lt;h4&gt;Write image to http.ResponseWriter:&lt;/h4&gt;&lt;p&gt;Now that we have a proper image, we need to send an HTTP response with it. I found two ways to do this:&lt;br&gt;&lt;h5&gt;Option 1: Send an HTTP response of type image/jpeg:&lt;/h5&gt;The first option is to write an HTTP response of type &lt;code&gt;image/jpeg&lt;/code&gt; or any format you want. This is what &lt;code&gt;WriteImage&lt;/code&gt; does:&lt;br&gt;                &lt;pre class=&#34;prettyprint&#34;&gt;    // writeImage encodes an image &#39;img&#39; in jpeg format and writes it into ResponseWriter.&lt;br&gt;    func writeImage(w http.ResponseWriter, img *image.Image) {&lt;br&gt;&lt;br&gt;        buffer := new(bytes.Buffer)&lt;br&gt;        if err := jpeg.Encode(buffer, *img, nil); err != nil {&lt;br&gt;            log.Println(&#34;unable to encode image.&#34;)&lt;br&gt;        }&lt;br&gt;&lt;br&gt;        w.Header().Set(&#34;Content-Type&#34;, &#34;image/jpeg&#34;)&lt;br&gt;        w.Header().Set(&#34;Content-Length&#34;, strconv.Itoa(len(buffer.Bytes())))&lt;br&gt;        if _, err := w.Write(buffer.Bytes()); err != nil {&lt;br&gt;            log.Println(&#34;unable to write image.&#34;)&lt;br&gt;        }&lt;br&gt;    }&lt;/pre&gt;This is quite straight forward:&lt;br&gt;                &lt;ul&gt;&lt;li&gt;Get an &lt;code&gt;Image&lt;/code&gt;&lt;/li&gt;&lt;li&gt;Encode it with a specific format.&lt;/li&gt;&lt;li&gt;Write the array of bytes to the &lt;code&gt;http.ResponseWriter&lt;/code&gt; using the &lt;a href=&#34;http://golang.org/pkg/net/http/#ResponseWriter&#34;&gt;Write&lt;/a&gt; function.&lt;/li&gt;&lt;/ul&gt;&lt;/p&gt;&lt;h5&gt;Option 2: Use a http/template to send the image:&lt;/h5&gt;&lt;br&gt;                &lt;p&gt;First we define a template that reads a raw image encoded in base 64:&lt;br&gt;                    &lt;pre class=&#34;prettyprint&#34;&gt;    var ImageTemplate string = `&amp;lt;!DOCTYPE html&amp;gt;&lt;br&gt;    &amp;lt;html lang=&amp;quot;en&amp;quot;&amp;gt;&amp;lt;head&amp;gt;&amp;lt;/head&amp;gt;&lt;br&gt;    &amp;lt;body&amp;gt;&amp;lt;img src=&amp;quot;data:image/jpg;base64,{{.Image}}&amp;quot;&amp;gt;&amp;lt;/body&amp;gt;`&lt;/pre&gt;Then we create a function that use this template:&lt;br&gt;                    &lt;pre class=&#34;prettyprint&#34;&gt;    // writeImageWithTemplate encodes an image &#39;img&#39; in jpeg format and writes it into ResponseWriter using a template.&lt;br&gt;    func writeImageWithTemplate(w http.ResponseWriter, img *image.Image) {&lt;br&gt;&lt;br&gt;        buffer := new(bytes.Buffer)&lt;br&gt;        if err := jpeg.Encode(buffer, *img, nil); err != nil {&lt;br&gt;            log.Println(&#34;unable to encode image.&#34;)&lt;br&gt;        }&lt;br&gt;&lt;br&gt;        str := base64.StdEncoding.EncodeToString(buffer.Bytes())&lt;br&gt;        if tmpl, err := template.New(&#34;image&#34;).Parse(ImageTemplate); err != nil {&lt;br&gt;            log.Println(&#34;unable to parse image template.&#34;)&lt;br&gt;        } else {&lt;br&gt;            data := map[string]interface{}{&#34;Image&#34;: str}&lt;br&gt;            if err = tmpl.Execute(w, data); err != nil {&lt;br&gt;                log.Println(&#34;unable to execute template.&#34;)&lt;br&gt;            }&lt;br&gt;        }&lt;br&gt;    }&lt;/pre&gt;Notice that we are using the package &lt;a href=&#34;http://golang.org/pkg/encoding/base64/&#34;&gt;encoding/base64&lt;/a&gt; in order to transform the image in a raw image.&lt;br&gt;&lt;br&gt;                    &lt;pre class=&#34;prettyprint&#34;&gt;str := base64.StdEncoding.EncodeToString(buffer.Bytes())&lt;/pre&gt;This is important, if you don&#39;t do this and only pass an array of bytes the browser will not be able to generate the image as it expects an image encoded in base64.&lt;br&gt;&lt;br&gt;                    If you look at the generated HTML source code you would see something like this:&lt;br&gt;&lt;br&gt;                    &lt;pre class=&#34;prettyprint&#34;&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;br&gt;&amp;lt;html lang=&amp;quot;en&amp;quot;&amp;gt;&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br&gt;&amp;lt;body&amp;gt;&lt;br&gt;&amp;lt;img src=&amp;quot;data:image/jpg;base64,/9j/2wCEAAgGBgc....KKAP/2Q==&amp;quot;&amp;gt;&lt;br&gt;&amp;lt;/body&amp;gt;&lt;/pre&gt;&lt;/p&gt;&lt;h4&gt;All together:&lt;/h4&gt;&lt;p&gt;Here is the full example also available in this &lt;a href=&#34;https://gist.github.com/santiaago/d6d681d14c5f3b3f5d69&#34;&gt;gist&lt;/a&gt;&lt;br&gt;&lt;br&gt;                        &lt;script src=&#34;https://gist.github.com/santiaago/d6d681d14c5f3b3f5d69.js&#34;&gt;&lt;/script&gt;&lt;br&gt;&lt;br&gt;                        &lt;b&gt;Some links that helped me find this:&lt;/b&gt;&lt;br&gt;                        &lt;ul&gt;&lt;li&gt;&lt;a href=&#34;http://stackoverflow.com/questions/22539999/how-to-display-raw-image-data-with-html-and-javascript&#34;&gt;How to display raw image data with html and javascript?&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://golang.org/pkg/image/&#34;&gt;package image&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://golang.org/pkg/encoding/base64/#example_Encoding_EncodeToString&#34;&gt;example encode to string&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://golang.org/pkg/net/http&#34;&gt;package http&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://blog.golang.org/go-imagedraw-package&#34;&gt;The Go image/draw package&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://golang.org/pkg/image/jpeg/&#34;&gt;package image/jpeg&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://blog.golang.org/go-image-package&#34;&gt;The Go image package&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/p&gt;&lt;small class=&#34;pull-right&#34;&gt;Santiaago&lt;/small&gt;</description>
      <link>https://www.sanarias.com/blog/1214PlayingwithimagesinHTTPresponseingolang/</link>
      <guid>https://www.sanarias.com/blog/1214PlayingwithimagesinHTTPresponseingolang/</guid>
      <pubDate>Fri, 26 Dec 2014 01:13:25 -0000</pubDate>
    </item>
    
    <item>
      <title>Merging local commits</title>
      <description>&lt;h3&gt;&lt;a href=&#34;/blog/1214Merginglocalcommits&#34; class=&#34;post-link&#34;&gt;Merging local commits&lt;/a&gt;&lt;/h3&gt;
&lt;h4&gt;&lt;small&gt;Dec 24, 2014&lt;/small&gt;&lt;/h4&gt;
&lt;p&gt;Usually when you work on a specific issue or a new feature you could end up with a &lt;i&gt;lot&lt;/i&gt; of local commits in order to track your progress as you try different things (even have multiple local branches). At the end of your day you have a final solution that you feel confortable with and you want to push that into &lt;code&gt;master&lt;/code&gt;.&lt;/p&gt;&lt;br&gt;&lt;p&gt;&lt;b&gt;But&lt;/b&gt; when you list your commits, &lt;code&gt;git log --oneline --decorate -10&lt;/code&gt; there might be many unnecessary commits. You probably do not want all that commit history in your working branch as that would blur your view on what was &lt;i&gt;really&lt;/i&gt; done.&lt;br&gt;In that case one option is to &lt;b&gt;merge&lt;/b&gt; the local commits and make a new set of commits that truly describe your work.&lt;/p&gt;&lt;br&gt;&lt;p&gt;After some investigation, I found this StackOverflow question: &lt;a href=&#34;http://stackoverflow.com/questions/8395213/how-to-merge-local-commits-at-a-develop-branch-in-git&#34;&gt;how to merge local commits at a develop branch in git&lt;/a&gt;&lt;br&gt;Here is what has worked for me:&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;git checkout mybranch&lt;br&gt;git merge-base mybranch origin/master&lt;br&gt;git reset commitId&lt;br&gt;git commit -m &#34;merge commits&#34;&lt;br&gt;git merge master mybranch&lt;br&gt;git push&lt;/pre&gt;Where the &lt;code&gt;commitId&lt;/code&gt; is something like &lt;code&gt;ac8fbef&lt;/code&gt;. This is the commit id of the &lt;i&gt;most recent&lt;/i&gt; commit before you start your work. To get this id you probably want to list the last x commits: &lt;code&gt;git log --oneline --decorate -10&lt;/code&gt;&lt;br&gt;So if I make 4 commits:&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;git commit -m &#34;start work on foo&#34;&lt;br&gt; git commit -m &#34;new method&#34;&lt;br&gt;git commit -m &#34;refactor&#34;&lt;br&gt;git commit -m &#34;changing foo to be foobar instead&#34;&lt;/pre&gt;And my git log would look like this:&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;git log --oneline --decorate -5&lt;br&gt;80a16bc changing foo to be foobar instead&lt;br&gt;987589e refactor&lt;br&gt;16d224d new method&lt;br&gt;2246fc6 start work on foo&lt;br&gt;ac8fbef fixes error on bar&lt;/pre&gt;In this case I want to make a &lt;code&gt;git reset ac8fbef&lt;/code&gt;. This will put &lt;b&gt;all&lt;/b&gt; the changes that were done on &lt;code&gt;mybranch&lt;/code&gt; into the &lt;b&gt;modified files&lt;/b&gt; and remove the commits I made locally.&lt;/p&gt;&lt;br&gt;&lt;p&gt;If you do a &lt;code&gt;git status&lt;/code&gt;, you will see all the files that you changed during your day of work (during the 4 commits I took as examples). You can now split those changes into separate and concise commits and push that into &lt;code&gt;master&lt;/code&gt; or any branch you are working on. Your commit history is clean and does not have unnecessary commits!&lt;/p&gt;&lt;small class=&#34;pull-right&#34;&gt;Santiaago&lt;/small&gt;</description>
      <link>https://www.sanarias.com/blog/1214Merginglocalcommits/</link>
      <guid>https://www.sanarias.com/blog/1214Merginglocalcommits/</guid>
      <pubDate>Wed, 24 Dec 2014 13:26:27 -0000</pubDate>
    </item>
    
    <item>
      <title>Python script to insert lines in multiple files following a pattern</title>
      <description>&lt;h3&gt;&lt;a href=&#34;/blog/614Pythonscripttoinsertlinesinmultiplefilesfollowingapattern&#34; class=&#34;post-link&#34;&gt;Python script to insert lines in multiple files following a pattern&lt;/a&gt;&lt;/h3&gt;
&lt;h4&gt;&lt;small&gt;Jun 11, 2014&lt;/small&gt;&lt;/h4&gt;
&lt;p&gt;In the past days I had to build a script to insert some lines of code after a specific statement.&lt;br&gt;For example, I have the following file:&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;    a &gt; b&lt;br&gt;    a = foo(c)&lt;br&gt;    b = 2&lt;br&gt;    b = foo(b)&lt;br&gt;    foo&lt;br&gt;    bar&lt;br&gt;    foobar&lt;br&gt;    b = foo(v)&lt;br&gt;&lt;/pre&gt;And I need to add a line &lt;code&gt;variable = bar(42)&lt;/code&gt; after every line of &lt;code&gt;variable = foo(x)&lt;/code&gt;&lt;br&gt;&lt;br&gt;After that treatment my file would be like this:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;    a &gt; b&lt;br&gt;    a = foo(c)&lt;br&gt;    a = bar(42)&lt;br&gt;    b = 2&lt;br&gt;    b = foo(b)&lt;br&gt;    b = bar(42)&lt;br&gt;    foo&lt;br&gt;    bar&lt;br&gt;    foobar&lt;br&gt;    c = foo(v)&lt;br&gt;    c = bar(42)&lt;br&gt;&lt;/pre&gt;Also, I had to perform this task on multiple files at the same time, the files are listed in a visual studio search result that will look like this:&lt;br&gt;&lt;br&gt;&lt;pre clss=&#34;prettyprint&#34;&gt;    Find all &#34;foo(&#34;, Subfolders, Find Results 1, &#34;c:\&#34;, &#34;*.txt&#34;&lt;br&gt;      C:\file1.txt(2):a = foo(c)&lt;br&gt;      C:\file1.txt(4):b = foo(b)&lt;br&gt;      C:\file1.txt(8):c = foo(v)&lt;br&gt;      C:\file2.txt(2):a1 = foo(c)&lt;br&gt;      C:\file2.txt(4):a3 = foo(b)&lt;br&gt;      C:\file2.txt(8):variable1 = foo(v)&lt;br&gt;      Matching lines: 6    Matching files: 2    Total files searched: 214&lt;br&gt;      Find was stopped in progress.&lt;br&gt;&lt;/pre&gt;So we need a way to get the &lt;b&gt;set&lt;/b&gt; of files from the visual studio search result and then run the task on each of them.&lt;br&gt;&lt;br&gt;Here is the main algorithm where:&lt;/br&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;LINE_TO_INSERT&lt;/code&gt; is the line to insert, in this case &lt;code&gt; = bar(42)\n&lt;/code&gt;&lt;/li&gt;&lt;li&gt;&lt;code&gt;PATTERN_OF_LINE&lt;/code&gt; in the pattern after which we insert the line&lt;br&gt;In this case &lt;code&gt;&#39;\w+&#39; + &#39;\\ \\=\\ foo\\(&#39; + &#39;\w+&#39;&lt;/code&gt; which matches with &lt;code&gt;a = foo(b)&lt;/code&gt; for example&lt;/li&gt;&lt;li&gt;&lt;code&gt;PATTERN_OF_FILE&lt;/code&gt; is the pattern of the files we are looking for in the visual studio search result&lt;/li&gt;&lt;/ul&gt;The main function is &lt;code&gt;insert_in_files()&lt;/code&gt;.&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;    def insert_in_files():&lt;br&gt;    	&#39;&#39;&#39;Insert LINE_TO_INSERT after all lines with pattern PATTERN_OF_LINE in all files with PATTERN_OF_FILE&#39;&#39;&#39;&lt;br&gt;    	files = get_files(&#39;vs_search_output.txt&#39;)&lt;br&gt;    	for f in files:&lt;br&gt;    		insert_lines(f)&lt;br&gt;    	return None&lt;br&gt;&lt;/pre&gt;&lt;br&gt;&lt;b&gt;Build a set of files from a visual studio search result:&lt;/b&gt;&lt;br&gt;&lt;br&gt;Here is the code that will get the set of files in the search result.&lt;br&gt;Where &lt;code&gt;PATTERN_OF_FILE&lt;/code&gt; is equal to &lt;code&gt;&#39;C\\:\\\\\w+.txt&#39;&lt;/code&gt;. Which means any files with extension &lt;code&gt;.txt&lt;/code&gt; whose name is any word character.&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;    def get_files(path):&lt;br&gt;    	&#39;&#39;&#39;Return a set of files from a visual studio search output following a regexp pattern&lt;br&gt;    	&lt;br&gt;    	Format of visual studio search output is the following:&lt;br&gt;    	Find all &#34;pattern to search&#34;, Subfolders, Find Results 1, Entire Solution, &#34;&#34;&lt;/br&gt;    	  path_to_file.txt(1):		line with text&lt;/br&gt;    	  path_to_another_file.txt(22):		line1 some text&lt;/br&gt;    	  path_to_another_file.txt(29):		line2 some text&lt;/br&gt;    	Matching lines: 2    Matching files: 2    Total files searched: 205&lt;br&gt;    	&lt;br&gt;    	Args:&lt;br&gt;    		path: A path to a file with the search result&lt;br&gt;    		&lt;br&gt;    	Returns:&lt;br&gt;    		A set of all files in search result that match PATTERN_OF_FILE regular expression.&lt;br&gt;    	&#39;&#39;&#39;&lt;br&gt;    	from re import search&lt;br&gt;    	from re import escape&lt;br&gt;    &lt;br&gt;    	f = open(path, &#39;r&#39;)&lt;br&gt;    	# paths is a set structure with all the different files to look for.&lt;br&gt;    	paths = set()&lt;br&gt;    	count = 0&lt;br&gt;    	for line in f:&lt;br&gt;    		m = search(PATTERN_OF_FILE, line)&lt;br&gt;    		if m:&lt;br&gt;    			paths.add(m.group(0))&lt;br&gt;    	f.close()&lt;br&gt;    	return paths&lt;br&gt;&lt;/pre&gt;If you run this function you will get a set of files.&lt;br&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;&lt;code&gt;insert_in_files&lt;/code&gt; is the python file where the &lt;code&gt;get_files&lt;/code&gt; is located.&lt;/li&gt;&lt;li&gt;&lt;code&gt;vs_search_output.txt&lt;/code&gt; is the visual studio search result listed above.&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;In a python shell:&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;    &gt;&gt;&gt; import insert_in_files&lt;br&gt;    &gt;&gt;&gt; insert_in_files.get_files(&#34;vs_search_output.txt&#34;)&lt;br&gt;    {&#39;C:\\file1.txt&#39;, &#39;C:\\file2.txt&#39;}&lt;br&gt;    &gt;&gt;&gt;&lt;br&gt;&lt;/pre&gt;&lt;br&gt;&lt;b&gt;Search for pattern in file and insert a line:&lt;/b&gt;&lt;br&gt;&lt;br&gt;As a remainder here are the constants we use:&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;    PATTERN_OF_FILE = &#39;C\\:\\\\\w+.txt&#39;&lt;br&gt;    PATTERN_OF_LINE = &#39;\w+&#39; + &#39;\\ \\=\\ foo\\(&#39; + &#39;\w+&#39; 	# looking for pattern a = foo(b)&lt;br&gt;    FIRST_VAR = &#39;^\w+&#39;&lt;br&gt;    LINE_TO_INSERT = &#39; = bar(42)\n&#39;&lt;br&gt;&lt;/pre&gt;And here is the code to search in a file and insert a line:&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;    def insert_lines(path):&lt;br&gt;    	&#39;&#39;&#39;Insert a line with &#39;FIRST_VAR + LINE_TO_INSERT&#39; after each PATTERN_OF_LINE&lt;br&gt;    	&lt;br&gt;    	Go through a file looking for PATTERN_OF_LINE &lt;br&gt;    	if found, insert a line just after with &#39;FIRST_VAR + LINE_TO_INSERT&#39;&lt;br&gt;    	&lt;br&gt;    	Args:&lt;br&gt;    		path: a path to a file to search and insert lines if a match is found&lt;br&gt;    	Returns:&lt;br&gt;    		None&lt;br&gt;    	&#39;&#39;&#39;&lt;br&gt;    	from re import escape&lt;br&gt;    	from re import search&lt;br&gt;    	fin = open(path, &#39;r&#39;)&lt;br&gt;    	open(&#39;output&#39;, &#39;w&#39;).close() 	# clear file before processing&lt;br&gt;    	fout = open(&#39;output&#39;, &#39;w&#39;)&lt;br&gt;    	&lt;br&gt;    	for line in fin:&lt;br&gt;    		fout.write(line)&lt;br&gt;    		match = search(PATTERN_OF_LINE, line)&lt;br&gt;    		if match:&lt;br&gt;    			res = search(FIRST_VAR, line)&lt;br&gt;    			if res:&lt;br&gt;    				fout.write(res.group(0) + LINE_TO_INSERT)&lt;br&gt;    	fin.close()&lt;br&gt;    	fout.close()&lt;br&gt;    	# save file&lt;br&gt;    	ftemp = open(&#39;output&#39;, &#39;r&#39;)&lt;br&gt;    	try:&lt;br&gt;    		open(path, &#39;w&#39;).close() 	# clear file before processing&lt;br&gt;    	except:&lt;br&gt;    		print(&#39;cannot write in file:&#39;, path)&lt;br&gt;    		ftemp.close()&lt;br&gt;    		return None&lt;br&gt;    	f = open(path, &#39;w&#39;)&lt;br&gt;    	for line in ftemp:&lt;br&gt;    		f.write(line)&lt;br&gt;    	f.close()&lt;br&gt;    	ftemp.close()&lt;br&gt;    	return None&lt;br&gt;&lt;/pre&gt;I made a &lt;a href=&#34;https://gist.github.com/santiaago/97f21023674c7c9af452&#34;&gt;gist&lt;/a&gt; with all the files and scripts.&lt;br&gt;&lt;small class=&#34;pull-right&#34;&gt;Santiaago&lt;/small&gt;&lt;br&gt;&lt;/p&gt;</description>
      <link>https://www.sanarias.com/blog/614Pythonscripttoinsertlinesinmultiplefilesfollowingapattern/</link>
      <guid>https://www.sanarias.com/blog/614Pythonscripttoinsertlinesinmultiplefilesfollowingapattern/</guid>
      <pubDate>Wed, 11 Jun 2014 08:59:48 -0000</pubDate>
    </item>
    
    <item>
      <title>Access your GAE web application from your smartphone</title>
      <description>&lt;h3&gt;&lt;a href=&#34;/blog/514AccessyourGAEwebapplicationfromyoursmartphone&#34; class=&#34;post-link&#34;&gt;Access your GAE web application from your smartphone&lt;/a&gt;&lt;/h3&gt;
&lt;h4&gt;&lt;small&gt;May 12, 2014&lt;/small&gt;&lt;/h4&gt;
&lt;p&gt;If you are building a web application using Google App Engine you might be interested in testing it with your smartphone &lt;small&gt;( &lt;b&gt;note:&lt;/b&gt; for this post, we suppose your appengine server and your smartphone are conected to the same network)&lt;/small&gt;. This is how I manage to do it.&lt;br&gt;&lt;br&gt;&lt;b&gt;Get your IP:&lt;/b&gt;&lt;br&gt;&lt;br&gt;First thing to do it to get the &lt;b&gt;IP&lt;/b&gt; where the server is running (in this case &lt;code&gt;192.168.1.X&lt;/code&gt;):&lt;br&gt;On a mac use a &lt;code&gt;ifconfig&lt;/code&gt;.&lt;br&gt;&lt;pre&gt;&gt; ifconfig&lt;br&gt;...&lt;br&gt;inet 192.168.1.X netmask 0xffffff00 broadcast 192.168.1.255&lt;br&gt;...&lt;/pre&gt;On a windows machine you would do a &lt;code&gt;ipconfig&lt;/code&gt;.&lt;br&gt;&lt;br&gt;&lt;b&gt;Run your app with the host parameter:&lt;/b&gt;&lt;br&gt;&lt;br&gt;Next you run the app with the &lt;code&gt;-host&lt;/code&gt; parameter set to &lt;code&gt;0.0.0.0&lt;/code&gt;.&lt;br&gt;&lt;br&gt;&lt;pre&gt;&gt; goapp serve -host=0.0.0.0&lt;br&gt;INFO     2014-04-26 10:25:33,296 devappserver2.py:764] Skipping SDK update check.&lt;br&gt;WARNING  2014-04-26 10:25:33,299 api_server.py:374] Could not initialize images API; you are likely missing the Python     &#34;PIL&#34; module.&lt;br&gt;INFO     2014-04-26 10:25:33,302 api_server.py:171] Starting API server at: http://localhost:53542&lt;br&gt;INFO     2014-04-26 10:25:33,305 dispatcher.py:182] Starting module &#34;default&#34; running at: http://0.0.0.0:8080&lt;br&gt;INFO     2014-04-26 10:25:33,307 admin_server.py:117] Starting admin server at: http://localhost:8000&lt;/pre&gt;&lt;b&gt;Access your web app from your smartphone:&lt;/b&gt;&lt;br&gt;&lt;br&gt;Finally access your web app from your smartphone with your IP like so: &lt;code&gt;http://192.168.1.X:8080/&lt;/code&gt;&lt;br&gt;&lt;br&gt;&lt;b&gt;Some links that helped me find this:&lt;/b&gt;&lt;br&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&#34;http://stackoverflow.com/questions/20156077/app-engine-go-sdk-web-server-running-in-vagrant-guest-with-port-forwarding-not&#34;&gt;App Engine Go SDK web server running in Vagrant guest (with port forwarding) not reachable from host&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;https://developers.google.com/appengine/docs/go/tools/devserver&#34;&gt;The Go Development Server&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;&lt;small class=&#34;pull-right&#34;&gt;Santiaago&lt;/small&gt;&lt;br&gt;&lt;/p&gt;&lt;br&gt;</description>
      <link>https://www.sanarias.com/blog/514AccessyourGAEwebapplicationfromyoursmartphone/</link>
      <guid>https://www.sanarias.com/blog/514AccessyourGAEwebapplicationfromyoursmartphone/</guid>
      <pubDate>Mon, 12 May 2014 09:29:07 -0000</pubDate>
    </item>
    
    <item>
      <title>2013 in review</title>
      <description>&lt;h3&gt;&lt;a href=&#34;/blog/4142013inreview&#34; class=&#34;post-link&#34;&gt;2013 in review&lt;/a&gt;&lt;/h3&gt;
&lt;h4&gt;&lt;small&gt;Apr 29, 2014&lt;/small&gt;&lt;/h4&gt;
&lt;p&gt;As in previews years, I would like to make a review of the year 2013. It is a little late as April 2014 is passing by... but it is never to late right?&lt;br&gt;&lt;br&gt;&lt;b&gt;MOOCs&lt;/b&gt;:&lt;br&gt;As a fan of MOOCs, I acomplished some online courses. Not as much as last year, but still, I made some pretty interesting courses:&lt;br&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&#34;http://work.caltech.edu/telecourse.html&#34;&gt;Learning from Data&lt;/a&gt; from &lt;b&gt;Caltech&lt;/b&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;https://www.coursera.org/course/nlangp&#34;&gt;Natural Language processing&lt;/a&gt; from Coursera&lt;/li&gt;&lt;/ul&gt; &lt;br&gt;Both github repositories are available &lt;a href=&#34;https://github.com/santiaago/caltech.ml&#34;&gt;here&lt;/a&gt; and &lt;a href=&#34;https://github.com/santiaago/coursera.nlangp&#34;&gt;here&lt;/a&gt;. Both of them were done in &lt;b&gt;Python&lt;/b&gt;.&lt;br&gt;&lt;br&gt;&lt;b&gt;Unfinished MOOCs&lt;/b&gt;:&lt;br&gt;&lt;br&gt;I hate to have this section but some times you are just not able to find the time to accomplish everything you set yourself to. I stared two other MOOCs. One on &lt;a href=&#34;https://class.coursera.org/ml-003&#34;&gt;Machine Learning&lt;/a&gt; and a &lt;a href=&#34;https://www.coursera.org/course/cplusplus4c&#34;&gt;c++ course&lt;/a&gt; both from &lt;a href=&#34;https://www.coursera.org/&#34;&gt;Coursera&lt;/a&gt;. I did not have enough time to finish them for different reasons. I might re-try them again at some point in time.&lt;br&gt;&lt;br&gt;&lt;b&gt;Learning Go&lt;/b&gt;:&lt;br&gt;&lt;br&gt;I set this as one of my goals for 2013, and I am really happy about my progression. During the year I did several projets and follow different tutorials in order to learn more about this language.&lt;br&gt;&lt;br&gt;Here is a list of some Github repos (or sub repos) in Go that I did (some of them are still in progress).&lt;br&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&#34;https://github.com/santiaago/go-samples&#34;&gt;go-samples&lt;/a&gt;&lt;/li&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&#34;https://github.com/santiaago/go-samples/tree/master/gotour&#34;&gt;gotour&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;https://github.com/santiaago/go-samples/tree/master/gowiki&#34;&gt;gowiki&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;https://github.com/santiaago/go-samples/tree/master/pluralsight&#34;&gt;pluralsight&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;li&gt;&lt;a href=&#34;https://github.com/santiaago/go-gae-helloworld&#34;&gt;simple web app in go and google app engine&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;https://github.com/santiaago/udacity.cs253.go&#34;&gt;porting udacity course cs253 from python to go&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;I also blogged about &lt;i&gt;porting of the udacity course cs253 from Python to Go&lt;/i&gt;,there are still some units to do :-)&lt;br&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&#34;http://www.sanarias.com/blog/613PortingUdacityCS253toGopart1&#34;&gt;Porting Udacity CS253 to Go part1&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://www.sanarias.com/blog/613PortingUdacityCS253toGopart2&#34;&gt;Porting Udacity CS253 to Go part2&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://www.sanarias.com/blog/713PortingUdacityCS253toGopart3&#34;&gt;Porting Udacity CS253 to Go part3&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;&lt;b&gt;Introduction to Dart&lt;/b&gt;:&lt;br&gt;&lt;br&gt;I started to play around with Dart and participate in several sessions (more than 10 I would say) of &lt;b&gt;#pairwithme&lt;/b&gt; proposed by &lt;a href=&#34;https://plus.google.com/+ChrisStrom/posts&#34;&gt;Chris Strom&lt;/a&gt; on his &lt;a href=&#34;https://github.com/eee-c/ice-code-editor&#34;&gt;ice code editor&lt;/a&gt;. This was really fun and I learned a lot, &lt;a href=&#34;http://www.sanarias.com/blog/513LearningDartthroughpairingsessions&#34;&gt;here&lt;/a&gt; I blogged about my experience doing this. I had to stop the &lt;b&gt;#pariwithme&lt;/b&gt; sessions because of the time difference between France and the US, but I would recommend it to anyone willing to learn some Dart and help Chris on his project.&lt;br&gt;&lt;br&gt;&lt;b&gt;Learning AngularJS&lt;/b&gt;:&lt;br&gt;&lt;br&gt;Angular was also one of my goals for 2013, I felt that I was missing knowledge in the web frontend, it is the place where I usually get stucked the most. I have learned a lot on angularjs and also on javascript and how to build modern web apps. &lt;a href=&#34;http://pluralsight.com/courses/angularjs-fundamentals&#34;&gt;This&lt;/a&gt; pluralsight course helped me with this, though it is not the best pluralsight course and it really goes fast on some aspects you wish they could have taken more time to address. I build a &lt;a href=&#34;https://github.com/santiaago/angularjs-samples&#34;&gt;repo&lt;/a&gt; for this course that helps me remember how to do certain things, though it is not up to date as it was done on April 2013 and the angular versions go pretty fast. Right now I am building a project with Angularjs on the frontend and Go and Google App Engine on the backend. The project is really nice, and it is planned to go live in 2014. More on that later. &lt;br&gt;&lt;br&gt;&lt;b&gt;More courses, more learning!&lt;/b&gt;&lt;br&gt;&lt;br&gt;Around July I attended a one week &lt;a href=&#34;http://grammars.grlmc.com/SSTiC2013/index.php&#34;&gt;summer school&lt;/a&gt; in Tarragona, Spain which addressed different themes in the computer science world, this was really great and I learned a lot from it. My main highlights on this week where the following courses:&lt;br&gt;&lt;ul&gt;&lt;li&gt;Web Search and Advertising by Professor Prabhakar Raghavan from Google, where we build a &lt;a href=&#34;http://www.sanarias.com/search&#34;&gt;search engine&lt;/a&gt; in python. I made a prototype in Python and Google App Engine &lt;a href=&#34;http://www.sanarias.com/search&#34;&gt;here&lt;/a&gt;.&lt;/li&gt;&lt;li&gt;Deep Learning of Representations by professor Yoshua Bengio, on Deep Learning in general.&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;Let&#39;s see what 2014 brings. :-)&lt;br&gt;&lt;small class=&#34;pull-right&#34;&gt;Santiaago&lt;/small&gt;&lt;br&gt;&lt;/p&gt;</description>
      <link>https://www.sanarias.com/blog/4142013inreview/</link>
      <guid>https://www.sanarias.com/blog/4142013inreview/</guid>
      <pubDate>Tue, 29 Apr 2014 11:43:36 -0000</pubDate>
    </item>
    
    <item>
      <title>Order dates chronologically in Go</title>
      <description>&lt;h3&gt;&lt;a href=&#34;/blog/214OrderdateschronologicallyinGo&#34; class=&#34;post-link&#34;&gt;Order dates chronologically in Go&lt;/a&gt;&lt;/h3&gt;
&lt;h4&gt;&lt;small&gt;Feb 19, 2014&lt;/small&gt;&lt;/h4&gt;
&lt;p&gt;Recently I had to order an array of structures based on a &lt;b&gt;time.Time&lt;/b&gt; field. Doing this was surprisingly easy in Go, so much that it is worth a small post :-)&lt;br&gt;Imagine you have a structure that contains a Date field, let’s use a simple Post structure as an example:&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;type Post struct {&lt;br&gt;    Id       int64&lt;br&gt;    Date     time.Time&lt;br&gt;    Title    string&lt;br&gt;    Content  string&lt;br&gt;}&lt;br&gt;&lt;/pre&gt;There is a chance that you will also have a container for those Posts in your code.&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;posts := []Post{p1, p2, p3, p4}&lt;/pre&gt;Now, how can you sort this array by dates? If you search for &lt;code&gt;sort array in Go&lt;/code&gt; you will get as first result the &lt;a href=&#34; http://golang.org/pkg/sort/&#34;&gt;sort package&lt;/a&gt; with some great examples. Sorting by a &lt;b&gt;time.Time&lt;/b&gt; type is not different, all you need is to change the sort implementation for Time type.&lt;br&gt;So for this specific example we need to implement &lt;code&gt;Len&lt;/code&gt;, &lt;code&gt;Swap&lt;/code&gt; and &lt;code&gt;Less&lt;/code&gt;. For the &lt;code&gt;Less&lt;/code&gt; function, use the &lt;a href=&#34;http://golang.org/pkg/time/#Time.Before&#34;&gt;Before&lt;/a&gt; function available in the &lt;b&gt;time&lt;/b&gt; package.&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;type ByDate []Post&lt;br&gt;&lt;br&gt;func (a ByDate) Len() int           { return len(a) }&lt;br&gt;func (a ByDate) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }&lt;br&gt;func (a ByDate) Less(i, j int) bool { return a[i].Date.Before(a[j].Date) }&lt;br&gt;&lt;/pre&gt;Finally, just call the sort function:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;sort.Sort(ByDate(posts))&lt;/pre&gt;I made a Gist &lt;a href=&#34;https://gist.github.com/santiaago/9027077#file-sortbydate-go&#34;&gt;sortbydate.go&lt;/a&gt; with a simple example, an array of dates, and as you can see this is easily extendable.&lt;/p&gt;&lt;br&gt;&lt;script src=&#34;https://gist.github.com/santiaago/9027077.js&#34;&gt;&lt;/script&gt;&lt;br&gt;</description>
      <link>https://www.sanarias.com/blog/214OrderdateschronologicallyinGo/</link>
      <guid>https://www.sanarias.com/blog/214OrderdateschronologicallyinGo/</guid>
      <pubDate>Wed, 19 Feb 2014 13:27:49 -0000</pubDate>
    </item>
    
    <item>
      <title>Porting Udacity CS253 to Go part3</title>
      <description>&lt;h3&gt;&lt;a href=&#34;/blog/713PortingUdacityCS253toGopart3&#34; class=&#34;post-link&#34;&gt;Porting Udacity CS253 to Go part3&lt;/a&gt;&lt;/h3&gt;
&lt;h4&gt;&lt;small&gt;Jul 12, 2013&lt;/small&gt;&lt;/h4&gt;
&lt;p&gt;On this post I continue to port &lt;a href=&#34;https://www.udacity.com&#34;&gt;Udacity&lt;/a&gt; course &lt;a href=&#34;https://www.udacity.com/course/cs253&#34;&gt;CS253 Web Development&lt;/a&gt; from python to &lt;a href=&#34;http://golang.org/&#34;&gt;Go&lt;/a&gt;. This time I am working on Unit 3. This is a long unit with a lot of ground so let&#39;s get started.&lt;br&gt;You can check out my previous blog post if you haven&#39;t done it yet:&lt;ul&gt;&lt;li&gt;&lt;a href=&#34;http://www.sanarias.com/blog/613PortingUdacityCS253toGopart1&#34;&gt;Porting Udacity CS253 to Go part1&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://www.sanarias.com/blog/613PortingUdacityCS253toGopart2&#34;&gt;Porting Udacity CS253 to Go part2&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;The code is at the following locations:&lt;br&gt;&lt;ul&gt;&lt;li&gt;The python &lt;a href=&#34;https://github.com/santiaago/udacity.cs253&#34;&gt;repository&lt;/a&gt;.&lt;/li&gt;&lt;li&gt;The Go &lt;a href=&#34;https://github.com/santiaago/udacity.cs253.go&#34;&gt;repository&lt;/a&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;b&gt;Note&lt;/b&gt;: Feel free to comment and share your thoughts, I will be more than happy to update and improve my code and learn to better develop in Go.&lt;br&gt;&lt;br&gt;This unit has three parts. The first part concerns the new Post handler which let you create a Post Entity in the &lt;a href=&#34;https://developers.google.com/appengine/docs/go/datastore/&#34;&gt;Datastore&lt;/a&gt;. The second part is about the Permalink handler, this deals with RegexHandlers, &lt;a href=&#34;https://developers.google.com/appengine/docs/go/memcache/&#34;&gt;memcache&lt;/a&gt; and &lt;a href=&#34;http://golang.org/pkg/html/template/&#34;&gt;templates&lt;/a&gt;. The third part deals with the FrontBlog Handler which displays the most recent posts.&lt;/p&gt;&lt;br&gt;&lt;h4&gt;1. NewPostHandler:&lt;/h4&gt;&lt;br&gt;&lt;p&gt;The main architecture of the &lt;b&gt;NewPostHandler&lt;/b&gt; is the following:&lt;br&gt;A &lt;code&gt;GET&lt;/code&gt; method to display the Form.&lt;br&gt;A &lt;code&gt;POST&lt;/code&gt; method to read the html Form, create the post and do a redirection to the created post.&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;// NewPostHandler is the HTTP handler to create a new Post&lt;br&gt;func NewPostHandler(w http.ResponseWriter, r *http.Request){&lt;br&gt;	c := appengine.NewContext(r)&lt;br&gt;	if r.Method == &#34;GET&#34; {&lt;br&gt;		// display the form&lt;br&gt;	}else if r.Method == &#34;POST&#34;{&lt;br&gt;		// read the html form&lt;br&gt;		// create a post entity&lt;br&gt;		// redirect to the created post&lt;br&gt;	}&lt;br&gt;}&lt;/pre&gt;&lt;/p&gt;&lt;br&gt;&lt;h5&gt;1.1 The GET method:&lt;/h5&gt;&lt;br&gt;&lt;p&gt;The &lt;b&gt;NewPostHandler&lt;/b&gt; displays a simple Form to create a new post. This is how it looks like:&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;&amp;lt;form method=&amp;quot;post&amp;quot;&amp;gt;&lt;br/&gt;  &amp;lt;label&amp;gt;&lt;br/&gt;    &amp;lt;div&amp;gt;subject&amp;lt;/div&amp;gt;&lt;br/&gt;    &amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;subject&amp;quot; value=&amp;quot;{{.Subject}}&amp;quot;&amp;gt;&lt;br/&gt;  &amp;lt;/label&amp;gt;&lt;br/&gt;  &amp;lt;label&amp;gt;&lt;br/&gt;    &amp;lt;div&amp;gt;content&amp;lt;/div&amp;gt;&amp;lt;textarea name=&amp;quot;content&amp;quot;&amp;gt;{{.Content}}&amp;lt;/textarea&amp;gt;&lt;br/&gt;  &amp;lt;/label&amp;gt;&lt;br/&gt;  &amp;lt;div class=&amp;quot;error&amp;quot;&amp;gt;{{.Error}}&amp;lt;/div&amp;gt;&lt;br/&gt;  &amp;lt;br&amp;gt;&lt;br/&gt;  &amp;lt;input type=&amp;quot;submit&amp;quot;&amp;gt;&lt;br/&gt;&amp;lt;/form&amp;gt;&lt;br/&gt;&lt;/pre&gt;This template goes hand in hand with this type structure to hold the new post information:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;// NewPostForm is the type used to hold the new post information.&lt;br&gt;type NewPostForm struct{&lt;br&gt;	Subject string &lt;br&gt;	Content string&lt;br&gt;	Error string&lt;br&gt;}&lt;/pre&gt;We display the Form by calling &lt;b&gt;writeNewPostForm&lt;/b&gt;:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;// executes the newpost.html template with NewPostForm type as param.&lt;br&gt;func writeNewPostForm(c appengine.Context, w http.ResponseWriter, postForm *NewPostForm){&lt;br&gt;	tmpl, err := template.ParseFiles(&#34;templates/newpost.html&#34;)&lt;br&gt;	if err != nil{&lt;br&gt;		c.Errorf(err.Error())&lt;br&gt;	}&lt;br&gt;	err = tmpl.Execute(w,postForm)&lt;br&gt;	if err != nil{&lt;br&gt;		c.Errorf(err.Error())&lt;br&gt;	}&lt;br&gt;}&lt;/pre&gt;&lt;/p&gt;&lt;br&gt;&lt;h5&gt;1.2 The POST method:&lt;/h5&gt;&lt;br&gt;&lt;p&gt;As always we read the information from the form by calling &lt;code&gt;r.FormValue()&lt;/code&gt;, then as in the previous unit we check the validity of the inputs before doing any further work.&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;postForm := NewPostForm{&lt;br&gt;	r.FormValue(&#34;subject&#34;),&lt;br&gt;	r.FormValue(&#34;content&#34;),&lt;br&gt;	&#34;&#34;,&lt;br&gt;}&lt;br&gt;if !(tools.IsStringValid(postForm.Subject) &amp;&amp; &lt;br&gt;	tools.IsStringValid(postForm.Content)){&lt;br&gt;&lt;br&gt;	postForm.Error = &#34;We need to set both a subject and some content&#34;&lt;br&gt;	writeNewPostForm(w, &amp;postForm)&lt;br&gt;}else{&lt;br&gt;	// create a blog post here.&lt;br&gt;	// ...&lt;br&gt;}&lt;/pre&gt;Once we are sure about the inputs, we can create a blog post. The first step, as for the python version, is to have an entity Post:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;// Post is the type used to hold the Post information.&lt;br&gt;type Post struct {&lt;br&gt;	Id int64&lt;br&gt;	Subject string&lt;br&gt;	Content string&lt;br&gt;	Created time.Time&lt;br&gt;}&lt;/pre&gt;&lt;b&gt;Note&lt;/b&gt;: You will need to import &lt;code&gt;&#34;appengine/datastore&#34;&lt;/code&gt; to perform the following operations.&lt;br&gt;To create an entity Post, I start by creating an ID, a Key and then I persist the Post entity via a Put method.&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;postID, _, _ := datastore.AllocateIDs(c, &#34;Post&#34;, nil, 1)&lt;br&gt;key := datastore.NewKey(c, &#34;Post&#34;, &#34;&#34;, postID, nil)&lt;br&gt;p := models.Post{&lt;br&gt;	postID,&lt;br&gt;	postForm.Subject,&lt;br&gt;	postForm.Content,&lt;br&gt;	time.Now(),&lt;br&gt;}&lt;br&gt;key, err := datastore.Put(c, key, &amp;p)&lt;br&gt;if err != nil {&lt;br&gt;	http.Error(w, err.Error(), http.StatusInternalServerError)&lt;br&gt;	return&lt;br&gt;}&lt;/pre&gt;I decided to store the postID into the entity, it is easiear the retreive it later in order to perform a permalink redirect &lt;code&gt;&#34;/blog/postid&#34;&lt;/code&gt;. In python this is easier as you can just do the following: &lt;code&gt;p.key().id()&lt;/code&gt;&lt;br&gt;In python this is how I created the Post:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;def post(self):&lt;br&gt;    user_subject = self.request.get(&#39;subject&#39;)&lt;br&gt;    user_content = self.request.get(&#39;content&#39;)&lt;br&gt;    &lt;br&gt;    subject = valid_str(user_subject)&lt;br&gt;    content = valid_str(user_content)&lt;br&gt;    if not(subject and content):&lt;br&gt;        self.write_form(&#34;We need to set both a subject and some content&#34;,user_subject,user_content)&lt;br&gt;    else:&lt;br&gt;        p = Post(parent = blog_key(), subject = user_subject,content = user_content)&lt;br&gt;        &lt;br&gt;        p.put()&lt;br&gt;        #redirect to permalink&lt;br&gt;        self.redirect(&#34;/unit3/blog/%s&#34; % str(p.key().id()))&lt;/pre&gt; where &lt;b&gt;blog_key&lt;/b&gt; returns a Key for the blog entity:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;def blog_key(name = &#39;default&#39;):&lt;br&gt;      return db.Key.from_path(&#39;blogs&#39;, name)&lt;/pre&gt;Next step now, perform a redirection to the postID permalink:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;// build url and redirect&lt;br&gt;permalinkURL := &#34;/blog/&#34;+strconv.FormatInt(p.Id,10)&lt;br&gt;http.Redirect(w, r, permalinkURL, http.StatusFound)&lt;/pre&gt;the Post entity is in the datastore now. We can now deal with the Permalink handler.&lt;/p&gt;&lt;br&gt;&lt;h4&gt;2. Permalink&lt;/h4&gt;&lt;br&gt;&lt;br&gt;&lt;h5&gt;2.1 Regex handlers&lt;/h5&gt;&lt;br&gt;&lt;p&gt;The first thing to work on when starting the permalink is how to dispatch the handlers.&lt;br&gt;In python this is easily done as the &lt;b&gt;WSGIApplication&lt;/b&gt; is able to parse regular expressions.&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;(&#39;/unit3/blog/?&#39;,BlogFront),&lt;br&gt;(&#39;/unit3/blog/newpost&#39;,NewPostHandler),&lt;br&gt;(&#39;/unit3/blog/([0-9]+)&#39;, Permalink),&lt;/pre&gt;In Go you cannot do this so I had to do some searching and this is the solution I came up with.&lt;br&gt;&lt;b&gt;Note&lt;/b&gt;: I used the following sources and made some changes in order to have the same behavior as in python:&lt;ul&gt;&lt;li&gt;&lt;a href=&#34;http://stackoverflow.com/questions/6564558/wildcards-in-the-pattern-for-http-handlefunc&#34;&gt;Wildcards in the pattern for http.HandleFunc&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;https://github.com/raymi/quickerreference&#34;&gt;QuickerReference&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;To use it, I create a &lt;b&gt;RegexpHandler&lt;/b&gt; type that has it&#39;s own &lt;b&gt;HandleFunc&lt;/b&gt; method.&lt;br&gt;This is how my &lt;code&gt;init()&lt;/code&gt; method looks like after using the &lt;code&gt;RegexpHandler&lt;/code&gt;:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;func init(){&lt;br&gt;	h := new( tools.RegexpHandler)&lt;br&gt;	&lt;br&gt;	h.HandleFunc(&#34;/&#34;,mainHandler)&lt;br&gt;	h.HandleFunc(&#34;/blog/?&#34;, unit3.BlogFrontHandler)&lt;br&gt;	h.HandleFunc(&#34;/blog/newpost&#34;, unit3.NewPostHandler)&lt;br&gt;	h.HandleFunc(&#34;/blog/[0-9]+/?&#34;,unit3.PermalinkHandler)&lt;br&gt;	&lt;br&gt;	http.Handle(&#34;/&#34;,h)&lt;br&gt;}&lt;/pre&gt;And this is how the &lt;b&gt;RegexpHandler&lt;/b&gt; looks like:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;package tools&lt;br&gt;import (&lt;br&gt;	&#34;net/http&#34;&lt;br&gt;	&#34;regexp&#34;&lt;br&gt;)&lt;br&gt;&lt;br&gt;type route struct {&lt;br&gt;    pattern *regexp.Regexp&lt;br&gt;    handler http.Handler&lt;br&gt;}&lt;br&gt;&lt;br&gt;type RegexpHandler struct {&lt;br&gt;    routes []*route&lt;br&gt;}&lt;br&gt;&lt;br&gt;func (h *RegexpHandler) Handler(pattern *regexp.Regexp, handler http.Handler) {&lt;br&gt;    h.routes = append(h.routes, &amp;route{pattern, handler})&lt;br&gt;}&lt;br&gt;&lt;br&gt;func (h *RegexpHandler) HandleFunc(strPattern string, handler func(http.ResponseWriter, *http.Request)) {&lt;br&gt;	// encapsulate string pattern with start and end constraints&lt;br&gt;	// so that HandleFunc would work as for Python GAE&lt;br&gt;	pattern := regexp.MustCompile(&#34;^&#34;+strPattern+&#34;$&#34;)&lt;br&gt;	h.routes = append(h.routes, &amp;route{pattern, http.HandlerFunc(handler)})&lt;br&gt;}&lt;br&gt;&lt;br&gt;func (h *RegexpHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {&lt;br&gt;	for _, route := range h.routes {&lt;br&gt;        if route.pattern.MatchString(r.URL.Path) {&lt;br&gt;            route.handler.ServeHTTP(w, r)&lt;br&gt;            return&lt;br&gt;        }&lt;br&gt;    }&lt;br&gt;    // no pattern matched; send 404 response&lt;br&gt;    http.NotFound(w, r)&lt;br&gt;}&lt;/pre&gt;Now that my &lt;b&gt;permalinkHandler&lt;/b&gt; is ready we can focus on the permalink handler implementation.&lt;/p&gt;&lt;br&gt;&lt;h5&gt;2.2 Retrieve the Post id&lt;/h5&gt;&lt;br&gt;&lt;p&gt;First thing to do is retrieve the id from the url. In python this is really easy as the id is a parameter of your &lt;code&gt;get()&lt;/code&gt; method:&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;class Permalink(renderHandler):&lt;br&gt;  def get(self, post_id):&lt;br&gt;      # do something awesome with post_id&lt;/pre&gt;In Go I did not find a way to do this so I had to retreive the id from the URL.&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;func PermalinkHandler(w http.ResponseWriter, r *http.Request){&lt;br&gt;&lt;br&gt;	if r.Method == &#34;GET&#34; {&lt;br&gt;		&lt;br&gt;		path := strings.Split(r.URL.String(), &#34;/&#34;)&lt;br&gt;		intID, _ := strconv.ParseInt(path[2], 0, 64)&lt;br&gt;		// do something awesome with the intID&lt;/pre&gt;Now that I have the post ID I can perform a query to the datastore and cache it into memcache if it does not yet exist.&lt;/p&gt;&lt;br&gt;&lt;h5&gt;2.3 Query the datastore and memcache&lt;/h5&gt;&lt;br&gt;&lt;p&gt;I created a function called &lt;b&gt;PostAndTimeByID&lt;/b&gt; which returns me the following structure:&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;// PostAndTime is the type used to hold an entity Post and it&#39;s &#34;cache hit time&#34; information.&lt;br&gt;type PostAndTime struct{&lt;br&gt;	Post Post&lt;br&gt;	Cache_hit_time time.Time&lt;br&gt;}&lt;/pre&gt;I had to work a little bit more on this as you need to encode the information you want to put in the cache. To &lt;a href=&#34;https://developers.google.com/appengine/docs/go/memcache/reference#Add&#34;&gt;add&lt;/a&gt; the &lt;b&gt;PostAndTime&lt;/b&gt; structure to memcache you have to encapsulate first in a memcache &lt;a href=&#34;https://developers.google.com/appengine/docs/go/memcache/reference#Item&#34;&gt;Item&lt;/a&gt;.&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;func Add(c appengine.Context, item *Item) error&lt;/pre&gt;An Item has a Key and its value. The value needs to be a slice of bytes. So to pass the bytes of the PostAndTime structure, I decided to use &lt;a href=&#34;http://golang.org/pkg/encoding/gob/&#34;&gt;gob&lt;/a&gt; (the other option was to use the &lt;a href=&#34;http://golang.org/pkg/encoding/json/&#34;&gt;json&lt;/a&gt; encoder) to encode and decode the bytes of my structure.&lt;/p&gt;&lt;br&gt;&lt;h5&gt;2.4 GOB encode and decode&lt;/h5&gt;&lt;br&gt;&lt;p&gt;Here is how to encode the structure to bytes with gob and set it to memcache:&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;// record information in cache for next time&lt;br&gt;mCache := new(bytes.Buffer)&lt;br&gt;encCache := gob.NewEncoder(mCache)&lt;br&gt;encCache.Encode(postAndTime)&lt;br&gt;		&lt;br&gt;postItem := &amp;memcache.Item{&lt;br&gt;	Key:   memcacheKey,&lt;br&gt;	Value: mCache.Bytes(),&lt;br&gt;}&lt;br&gt;if err := memcache.Add(c, postItem); err == memcache.ErrNotStored {&lt;br&gt;	c.Errorf(&#34;cs253: postAndTime with key %q already exists&#34;, item.Key)&lt;br&gt;} else if err != nil {&lt;br&gt;	c.Errorf(&#34;error adding item: %v&#34;, err)&lt;br&gt;}&lt;/pre&gt;&lt;b&gt;Note&lt;/b&gt;: You need to register the type you want to encode before doing the above code, else it will panic. This is done by calling the &lt;code&gt;Register&lt;/code&gt; method. It is important to mention that you cannot register a type more than once or it will panic as well so the best is to have this in the init function of your package as follows:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;func init(){&lt;br&gt;	gob.Register(PostAndTime{})&lt;br&gt;}&lt;/pre&gt;When the item is found in the memcache, you will have to do the opposite and decode the bytes into the structure. This is how I did it:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;//Memcache item found&lt;br&gt;var postAndTime PostAndTime&lt;br&gt;&lt;br&gt;pCache := bytes.NewBuffer(item.Value)&lt;br&gt;decCache := gob.NewDecoder(pCache)&lt;br&gt;decCache.Decode(&amp;postAndTime)&lt;/pre&gt;&lt;/p&gt;&lt;br&gt;&lt;h5&gt;2.5 Code of PostAndTimeByID&lt;/h5&gt;&lt;br&gt;&lt;p&gt;Here is the full code for &lt;b&gt;PostAndTimeByID&lt;/b&gt; function:&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;// PostAndTimeByID returns a PostAndTime for the requested id&lt;br&gt;func PostAndTimeByID(c appengine.Context, id int64)( PostAndTime){&lt;br&gt;	memcacheKey := &#34;posts_and_time&#34;+strconv.FormatInt(id, 10)&lt;br&gt;&lt;br&gt;	var postAndTime PostAndTime&lt;br&gt;	//query cache first with memcache key&lt;br&gt;	if item, err := memcache.Get(c, memcacheKey); err == memcache.ErrCacheMiss {&lt;br&gt;		//item not in the cache : will perform query instead&lt;br&gt;&lt;br&gt;		key := datastore.NewKey(c, &#34;Post&#34;, &#34;&#34;, id, nil)&lt;br&gt;&lt;br&gt;		if err := datastore.Get(c, key, &amp;postAndTime.Post); err != nil {&lt;br&gt;			c.Errorf(&#34;cs253: post not found : %v&#34;, err)&lt;br&gt;		}&lt;br&gt;		// get current hit time&lt;br&gt;		postAndTime.Cache_hit_time = time.Now()&lt;br&gt;		// record information in cache for next time&lt;br&gt;		mCache := new(bytes.Buffer)&lt;br&gt;		encCache := gob.NewEncoder(mCache)&lt;br&gt;		encCache.Encode(postAndTime)&lt;br&gt;		&lt;br&gt;		postItem := &amp;memcache.Item{&lt;br&gt;			Key:   memcacheKey,&lt;br&gt;			Value: mCache.Bytes(),&lt;br&gt;		}&lt;br&gt;		if err := memcache.Add(c, postItem); err == memcache.ErrNotStored {&lt;br&gt;			c.Errorf(&#34;cs253: postAndTime with key %q already exists&#34;, item.Key)&lt;br&gt;		} else if err != nil {&lt;br&gt;			c.Errorf(&#34;error adding item: %v&#34;, err)&lt;br&gt;		}&lt;br&gt;&lt;br&gt;	} else if err != nil {&lt;br&gt;		c.Errorf(&#34;cs253: Memcache error getting item: %v&#34;,err)&lt;br&gt;	} else {&lt;br&gt;		//Memcache item found&lt;br&gt;&lt;br&gt;		pCache := bytes.NewBuffer(item.Value)&lt;br&gt;		decCache := gob.NewDecoder(pCache)&lt;br&gt;		decCache.Decode(&amp;postAndTime)&lt;br&gt;	}	&lt;br&gt;	return postAndTime&lt;br&gt;}&lt;/pre&gt;Note some memcache operations like the Get method:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;memcache.Get(c, memcacheKey)&lt;/pre&gt; and Add method:&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;memcache.Add(c, postItem)&lt;/pre&gt;Also notice the datastore operation:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;datastore.Get(c, key, &amp;postAndTime.Post)&lt;/pre&gt;&lt;/p&gt;&lt;br&gt;&lt;h5&gt;2.6 Using multiple templates&lt;/h5&gt;&lt;br&gt;&lt;p&gt;Once you have the post and the memcache hit time you can render the permalink page. This time we are defining the template as a separate file and we are using two templates to do this, a permalink.html template for the structure of the page and a post.html template of the post information (the post.html will be used later on in another handler).&lt;br&gt;This is how the permalink.html template looks like:&lt;br&gt;&lt;pre class=&#34;prettyprint lang-html&#34;&gt;{{define &amp;quot;permalink&amp;quot;}}&lt;br/&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;br/&gt;&amp;lt;html&amp;gt;&lt;br/&gt;&amp;lt;head&amp;gt;&lt;br/&gt;  &amp;lt;link type=&amp;quot;text/css&amp;quot; rel=&amp;quot;stylesheet&amp;quot; href=&amp;quot;/static/main.css&amp;quot; /&amp;gt;&lt;br/&gt;  &amp;lt;title&amp;gt;CS 253 Blog in Go!&amp;lt;/title&amp;gt;&lt;br/&gt;&amp;lt;/head&amp;gt;&lt;br/&gt;&amp;lt;body&amp;gt;&lt;br/&gt;  &amp;lt;a href=&amp;quot;/blog&amp;quot; class=&amp;quot;main-title&amp;quot;&amp;gt;&lt;br/&gt;    Blog&lt;br/&gt;  &amp;lt;/a&amp;gt;&lt;br/&gt;  {{template &amp;quot;post&amp;quot; .Post}}&lt;br/&gt;  &amp;lt;div class=&amp;quot;age&amp;quot;&amp;gt;&lt;br/&gt;      queried {{.Cache_hit_time}} seconds&lt;br/&gt;    &amp;lt;/div&amp;gt;&lt;br/&gt;&amp;lt;/body&amp;gt;&lt;br/&gt;&amp;lt;/html&amp;gt;&lt;br/&gt;{{end}}&lt;br/&gt;&lt;/pre&gt;and the post.html template looks like this:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint lang-html&#34;&gt;{{define &amp;quot;post&amp;quot;}}&lt;br/&gt;&amp;lt;div class=&amp;quot;post&amp;quot;&amp;gt;&lt;br/&gt;  &amp;lt;div class=&amp;quot;post-heading&amp;quot;&amp;gt;&lt;br/&gt;    &amp;lt;div class=&amp;quot;post-title&amp;quot;&amp;gt;&lt;br/&gt;        &amp;lt;a href=&amp;quot;/blog/{{.Id}}&amp;quot; class=&amp;quot;post-link&amp;quot;&amp;gt;{{.Subject}}&amp;lt;/a&amp;gt;&lt;br/&gt;    &amp;lt;/div&amp;gt;&lt;br/&gt;&lt;br/&gt;    &amp;lt;div class=&amp;quot;post-date&amp;quot;&amp;gt;&lt;br/&gt;      {{.Created.Format &amp;quot;02-01-2006 15:04:05&amp;quot;}}&lt;br/&gt;    &amp;lt;/div&amp;gt;&lt;br/&gt;  &amp;lt;/div&amp;gt;&lt;br/&gt;&lt;br/&gt;  &amp;lt;div class=&amp;quot;post-content&amp;quot;&amp;gt;&lt;br/&gt;    {{.Content}}&lt;br/&gt;  &amp;lt;/div&amp;gt;&lt;br/&gt;&amp;lt;/div&amp;gt;&lt;br/&gt;{{end}}&lt;br/&gt;&lt;/pre&gt;Notice that I pass &lt;code&gt;.Post&lt;/code&gt; to the post template like this:&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;{{template &amp;quot;post&amp;quot; .Post}}&lt;/pre&gt;To render these two templates together we have to parse them first and then execute the permalink template.&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;// writePermalink executes the permalink.html template with a PostAndTime type as param.&lt;br&gt;func writePermalink(c appengine.Context, w http.ResponseWriter, p models.PostAndTime){&lt;br&gt;	tmpl, err := template.ParseFiles(&#34;templates/permalink.html&#34;,&#34;templates/post.html&#34;)&lt;br&gt;	if err != nil{&lt;br&gt;		c.Errorf(err.Error())&lt;br&gt;	}&lt;br&gt;	err = tmpl.ExecuteTemplate(w,&#34;permalink&#34;,p)&lt;br&gt;	if err !=nil{&lt;br&gt;		c.Errorf(err.Error())&lt;br&gt;	}&lt;br&gt;}&lt;/pre&gt;This is all concerning the permalink handler, next is the blog front handler where we display the first 10 blog posts.&lt;/p&gt;&lt;br&gt;&lt;h4&gt;3. BlogFrontHandler&lt;/h4&gt;&lt;br&gt;&lt;p&gt;The &lt;b&gt;BlogFrontHandler&lt;/b&gt; is a simple one. It performs a query for the recent posts and renders them. This is how it looks:&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;// BlogFrontHandler is the HTTP handler for displaying the most recent posts.&lt;br&gt;func BlogFrontHandler(w http.ResponseWriter, r *http.Request){&lt;br&gt;	c := appengine.NewContext(r)&lt;br&gt;	if r.Method == &#34;GET&#34; {&lt;br&gt;		posts := models.RecentPosts(c)&lt;br&gt;		writeBlog(w, posts)&lt;br&gt;	}&lt;br&gt;}&lt;/pre&gt;The &lt;b&gt;RecentPosts&lt;/b&gt; function performs a query in the datastore as follows:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;// RecentPosts returns a pointer to a slice of Posts.&lt;br&gt;// orderBy creation time, limit = 20&lt;br&gt;func RecentPosts(c appengine.Context)([]*Post){&lt;br&gt;	q := datastore.NewQuery(&#34;Post&#34;).Limit(20).Order(&#34;-Created&#34;)&lt;br&gt;	var posts []*Post&lt;br&gt;	if _, err := q.GetAll(c, &amp;posts); err != nil {&lt;br&gt;		c.Errorf(&#34;cs253: Error: %v&#34;,err)&lt;br&gt;		return nil&lt;br&gt;	}&lt;br&gt;	return posts&lt;br&gt;}&lt;/pre&gt;The writeBlog function uses two templates, the post.html template and the blog.html template.&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint lang-html&#34;&gt;{{define &amp;quot;blog&amp;quot;}}&lt;br/&gt;    &amp;lt;!-- some html content --&amp;gt;&lt;br/&gt;    &amp;lt;div id=&amp;quot;content&amp;quot;&amp;gt;&lt;br/&gt;      {{range $i, $p :=.}}&lt;br/&gt;      {{template &amp;quot;post&amp;quot; $p}}&lt;br/&gt;      {{end}}&lt;br/&gt;        &amp;lt;div class=&amp;quot;age&amp;quot;&amp;gt;&lt;br/&gt;            queried cache_last_hit seconds ago&lt;br/&gt;          &amp;lt;/div&amp;gt;&lt;br/&gt;    &amp;lt;/div&amp;gt;&lt;br/&gt;    &amp;lt;!-- some closing tags --&amp;gt;&lt;br/&gt;{{end}}&lt;/pre&gt;&lt;b&gt;writeBlog&lt;/b&gt; looks exactly as &lt;b&gt;writePost&lt;/b&gt; (I should factorize this at some point...)&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;// writeBlog executes the blog template with a slice of Posts.&lt;br&gt;func writeBlog(c appengine.Context, w http.ResponseWriter, posts []*models.Post){&lt;br&gt;	tmpl, err := template.ParseFiles(&#34;templates/blog.html&#34;,&#34;templates/post.html&#34;)&lt;br&gt;	if err != nil{&lt;br&gt;		c.Errorf(err.Error())&lt;br&gt;	}&lt;br&gt;	err = tmpl.ExecuteTemplate(w,&#34;blog&#34;,posts)&lt;br&gt;	if err != nil{&lt;br&gt;		c.Errorf(err.Error())&lt;br&gt;	}&lt;br&gt;}&lt;/pre&gt;This covers the third CS253 unit. Next time will be about Unit 4.&lt;/p&gt;</description>
      <link>https://www.sanarias.com/blog/713PortingUdacityCS253toGopart3/</link>
      <guid>https://www.sanarias.com/blog/713PortingUdacityCS253toGopart3/</guid>
      <pubDate>Fri, 12 Jul 2013 10:55:04 -0000</pubDate>
    </item>
    
    <item>
      <title>Porting Udacity CS253 to Go part2</title>
      <description>&lt;h3&gt;&lt;a href=&#34;/blog/613PortingUdacityCS253toGopart2&#34; class=&#34;post-link&#34;&gt;Porting Udacity CS253 to Go part2&lt;/a&gt;&lt;/h3&gt;
&lt;h4&gt;&lt;small&gt;Jun 25, 2013&lt;/small&gt;&lt;/h4&gt;
&lt;p&gt;On this post I continue to port &lt;a href=&#34;https://www.udacity.com&#34;&gt;Udacity&lt;/a&gt; course &lt;a href=&#34;https://www.udacity.com/course/cs253&#34;&gt;CS253 Web Development&lt;/a&gt; from python to &lt;a href=&#34;http://golang.org/&#34;&gt;Go&lt;/a&gt;. This time I am working on Unit 2. This is a small and simple unit, just to get warmed up for the next one.&lt;/p&gt;&lt;p&gt;You can check out my previous blog post if you haven&#39;t done it yet:&lt;br&gt;&lt;ul&gt;&lt;li&gt;&lt;a href=&#34;http://www.sanarias.com/blog/613PortingUdacityCS253toGopart1&#34;&gt;Porting Udacity CS253 to Go part1&lt;/a&gt;.&lt;/li&gt;&lt;/ul&gt;The code is at the following locations:&lt;br&gt;&lt;ul&gt;&lt;li&gt;The python &lt;a href=&#34;https://github.com/santiaago/udacity.cs253&#34;&gt;repository&lt;/a&gt;.&lt;/li&gt;&lt;li&gt;The Go &lt;a href=&#34;https://github.com/santiaago/udacity.cs253.go&#34;&gt;repository&lt;/a&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;&lt;b&gt;Note:&lt;/b&gt; Feel free to comment and share your thoughts, I will be more than happy to update and improve my code and learn to better develop in Go.&lt;/p&gt;&lt;p&gt;This unit has two parts. The first part is the Rot13 Handler which substitutes a string back and forth using the ROT13 cipher. The second part is a signup and welcome workflow.&lt;/p&gt;&lt;br&gt;&lt;h4&gt;Unit2: implementing Rot13&lt;/h4&gt;&lt;br&gt;&lt;p&gt;Some of you might already be familiar with &lt;a href=&#34;https://en.wikipedia.org/wiki/ROT13&#34;&gt;Rot13&lt;/a&gt;, it is a substitution cipher. The &lt;a href=&#34;http://tour.golang.org/#60&#34;&gt;Go Tour #60&lt;/a&gt; presents it as well.&lt;/p&gt;&lt;p&gt;In python the ROT13 cipher is built-in the library. This is how it looks in a Python shell:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;&gt;&gt;&gt; s = &#39;hello&#39;&lt;br&gt;&gt;&gt;&gt; s.encode(&#39;rot13&#39;)&lt;br&gt;&#39;uryyb&#39;&lt;br&gt;&lt;/pre&gt;As you see you just use the built-in &lt;a href=&#34;http://docs.python.org/2/library/stdtypes.html#str.encode&#34;&gt;encode()&lt;/a&gt; method with the string &lt;i&gt;rot13&lt;/i&gt; and that&#39;s it.&lt;br&gt;In Go I did not find anything like this and as I already did the Go Tour exercises I decided to reuse that.&lt;/p&gt;&lt;p&gt;I made some minor changes to the implementation so that the cipher method would return directly a string. This is what it looks like.&lt;pre class=&#34;prettyprint&#34;&gt;// Rot13 is the type used to hold the string to encode.&lt;br&gt;type Rot13 string&lt;br&gt;&lt;br&gt;// rot13 returns the rot13 substitution of single byte.&lt;br&gt;func rot13(b byte) byte{&lt;br&gt;	var first, second byte&lt;br&gt;	switch{&lt;br&gt;	case &#39;a&#39; &lt;= b &amp;&amp; b &lt;= &#39;z&#39;:&lt;br&gt;		first, second = &#39;a&#39;, &#39;z&#39;&lt;br&gt;	case &#39;A&#39; &lt;= b &amp;&amp; b &lt;= &#39;Z&#39;:&lt;br&gt;		first, second = &#39;A&#39;, &#39;Z&#39;&lt;br&gt;	default:&lt;br&gt;		return b&lt;br&gt;	}&lt;br&gt;	return (b - first + 13)%(second - first + 1) + first&lt;br&gt;}&lt;br&gt;&lt;br&gt;// Rot13 implement Encode function to perform ROT13 substitution.&lt;br&gt;func (r Rot13) Encode() string{&lt;br&gt;	n := len(r)&lt;br&gt;	t:= []byte(r)&lt;br&gt;	for i := 0; i &lt; n; i++{&lt;br&gt;		t[i] = rot13(t[i])&lt;br&gt;	}&lt;br&gt;	return string(t)&lt;br&gt;}&lt;/pre&gt;Now that the  type structure is ready we can focus on the Handler.&lt;/p&gt;&lt;br&gt;&lt;h4&gt;Unit 2: Rot13 Handler&lt;/h4&gt;&lt;br&gt;&lt;p&gt;This is what my Rot13Handler looks like in python:&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;class Rot13Handler(webapp2.RequestHandler):&lt;br&gt;    &lt;br&gt;    def write_form(self,rot13=&#34;&#34;):&lt;br&gt;        self.response.out.write(htmlRot13%{&#34;rot13&#34;:escape_html(rot13)})&lt;br&gt;        &lt;br&gt;    def get(self):&lt;br&gt;        self.write_form()&lt;br&gt;        &lt;br&gt;    def post(self):&lt;br&gt;        user_input = self.request.get(&#39;text&#39;)&lt;br&gt;        input_changed = user_input.encode(&#39;rot13&#39;)&lt;br&gt;        self.write_form(input_changed)&lt;br&gt;&lt;/pre&gt;And this is how it looks in Go:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;// Rot13Handler is the HTTP handler for encoding and decoding a string.&lt;br&gt;// (rot13(rot13(x)) = x )&lt;br&gt;func Rot13Handler(w http.ResponseWriter, r *http.Request){&lt;br&gt;	c := appengine.NewContext(r)&lt;br&gt;	if r.Method == &#34;GET&#34; {&lt;br&gt;		writeFormRot13(w, &#34;&#34;)&lt;br&gt;	} else if r.Method == &#34;POST&#34;{&lt;br&gt;		var r13 Rot13 = Rot13(r.FormValue(&#34;text&#34;))&lt;br&gt;		writeFormRot13(w, r13.Encode())&lt;br&gt;	}else{&lt;br&gt;		tools.Error404(w)&lt;br&gt;		return&lt;br&gt;	}&lt;br&gt;}&lt;/pre&gt;&lt;/p&gt;&lt;p&gt;As in &lt;b&gt;unit1&lt;/b&gt; I define an internal const string and a template to work on this unit.&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;var rot13Template = template.Must(template.New(&amp;quot;Rot13&amp;quot;).Parse(rot13HTML))&lt;/pre&gt; And this is what the const &lt;b&lt;&gt;rot13HTML&lt;/b&gt; looks like:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;&amp;lt;!DOCTYPE html&amp;gt;&amp;lt;html&amp;gt;&lt;br/&gt;    &amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Unit 2 Rot 13&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;&lt;br/&gt;    &amp;lt;body&amp;gt;&lt;br/&gt;        &amp;lt;h2&amp;gt;Enter some text to ROT13:&amp;lt;/h2&amp;gt;&lt;br/&gt;        &amp;lt;form method=&amp;quot;post&amp;quot;&amp;gt;&lt;br/&gt;            &amp;lt;textarea name=&amp;quot;text&amp;quot; style=&amp;quot;height: 100px; width: 400px;&amp;quot;&amp;gt;{{.Str}}&amp;lt;/textarea&amp;gt;&amp;lt;br&amp;gt;&lt;br/&gt;            &amp;lt;input type=&amp;quot;submit&amp;quot;&amp;gt;&lt;br/&gt;        &amp;lt;/form&amp;gt;&lt;br/&gt;    &amp;lt;/body&amp;gt;&lt;br/&gt;&amp;lt;/html&amp;gt;&lt;br/&gt;&lt;/pre&gt;Since this is a fairly simple example we can do this internally. For next units I am going to externalize the html into separate templates.&lt;br&gt;The &lt;b&gt;Rot13Handler&lt;/b&gt; checks the &lt;code&gt;POST&lt;/code&gt; and &lt;code&gt;GET&lt;/code&gt; method. On the &lt;code&gt;GET&lt;/code&gt; method it will simply display the blank form. On the &lt;code&gt;POST&lt;/code&gt; method we will get the form information as usual by doing&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;r.FormValue(&#34;text&#34;)&lt;/pre&gt; and then encode this by doing the ROT13 substitution &lt;code&gt;r13.Encode()&lt;/code&gt;.&lt;/p&gt;&lt;br&gt;&lt;h4&gt;Unit 2: Signup handler&lt;/h4&gt;&lt;br&gt;&lt;p&gt;The signup url has a simple signup form. When the form is submitted we verify its content and redirect on success and display the errors otherwise.&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;&amp;lt;!DOCTYPE html&amp;gt;&amp;lt;html&amp;gt;&lt;br/&gt;  &amp;lt;head&amp;gt;&lt;br/&gt;    &amp;lt;title&amp;gt;Sign Up&amp;lt;/title&amp;gt;&lt;br/&gt;    &amp;lt;style type=&amp;quot;text/css&amp;quot;&amp;gt;.label {text-align: right}.error {color: red}&amp;lt;/style&amp;gt;&lt;br/&gt;  &amp;lt;/head&amp;gt;&lt;br/&gt;  &amp;lt;body&amp;gt;&lt;br/&gt;	&amp;lt;h2&amp;gt;Signup&amp;lt;/h2&amp;gt;&lt;br/&gt;	&amp;lt;form method=&amp;quot;post&amp;quot;&amp;gt;&lt;br/&gt;	  &amp;lt;table&amp;gt;&lt;br/&gt;	    &amp;lt;tr&amp;gt;&lt;br/&gt;	      &amp;lt;td class=&amp;quot;label&amp;quot;&amp;gt;Username&amp;lt;/td&amp;gt;&lt;br/&gt;	      &amp;lt;td&amp;gt;&amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;username&amp;quot; value=&amp;quot;{{.Username}}&amp;quot;&amp;gt;&amp;lt;/td&amp;gt;&lt;br/&gt;	      &amp;lt;td class=&amp;quot;error&amp;quot;&amp;gt;{{.ErrorUser}}&amp;lt;/td&amp;gt;&lt;br/&gt;	    &amp;lt;/tr&amp;gt;&lt;br/&gt;	    &amp;lt;tr&amp;gt;&lt;br/&gt;	      &amp;lt;td class=&amp;quot;label&amp;quot;&amp;gt;Password&amp;lt;/td&amp;gt;&lt;br/&gt;	      &amp;lt;td&amp;gt;&amp;lt;input type=&amp;quot;password&amp;quot; name=&amp;quot;password&amp;quot; value=&amp;quot;{{.Password}}&amp;quot;&amp;gt;&amp;lt;/td&amp;gt;&lt;br/&gt;	      &amp;lt;td class=&amp;quot;error&amp;quot;&amp;gt;{{.ErrorPassword}}&amp;lt;/td&amp;gt;&lt;br/&gt;	    &amp;lt;/tr&amp;gt;&lt;br/&gt;	    &amp;lt;tr&amp;gt;&lt;br/&gt;	      &amp;lt;td class=&amp;quot;label&amp;quot;&amp;gt;Verify Password&amp;lt;/td&amp;gt;&lt;br/&gt;	      &amp;lt;td&amp;gt;&amp;lt;input type=&amp;quot;password&amp;quot; name=&amp;quot;verify&amp;quot; value=&amp;quot;{{.Verify}}&amp;quot;&amp;gt;&amp;lt;/td&amp;gt;&lt;br/&gt;	      &amp;lt;td class=&amp;quot;error&amp;quot;&amp;gt;{{.ErrorPasswordMatch}}&amp;lt;/td&amp;gt;&lt;br/&gt;	    &amp;lt;/tr&amp;gt;&lt;br/&gt;	    &amp;lt;tr&amp;gt;&lt;br/&gt;	      &amp;lt;td class=&amp;quot;label&amp;quot;&amp;gt;Email (optional)&amp;lt;/td&amp;gt;&lt;br/&gt;	      &amp;lt;td&amp;gt;&amp;lt;input type=&amp;quot;text&amp;quot; name=&amp;quot;email&amp;quot; value=&amp;quot;{{.Email}}&amp;quot;&amp;gt;&amp;lt;/td&amp;gt;&lt;br/&gt;	      &amp;lt;td class=&amp;quot;error&amp;quot;&amp;gt;{{.ErrorEmail}}&amp;lt;/td&amp;gt;&lt;br/&gt;	    &amp;lt;/tr&amp;gt;&lt;br/&gt;	  &amp;lt;/table&amp;gt;&lt;br/&gt;	  &amp;lt;input type=&amp;quot;submit&amp;quot;&amp;gt;&lt;br/&gt;	&amp;lt;/form&amp;gt;&lt;br/&gt;  &amp;lt;/body&amp;gt;&lt;br/&gt;&amp;lt;/html&amp;gt;&lt;br/&gt;&lt;/pre&gt;The &lt;code&gt;GET&lt;/code&gt; method is a simple execution of the signup template with no information in it. For the &lt;code&gt;POST&lt;/code&gt; method I decided to create a Signup type to hold the user&#39;s information and the possible errors it might have.&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;type Signup struct{&lt;br&gt;	Username string&lt;br&gt;	Password string&lt;br&gt;	Email string&lt;br&gt;	Verify string&lt;br&gt;	ErrorUser string&lt;br&gt;	ErrorPassword string&lt;br&gt;	ErrorPasswordMatch string&lt;br&gt;	ErrorEmail string&lt;br&gt;}&lt;/pre&gt;We retrieve the signup information as before done, by calling &lt;code&gt;r.FormValue&lt;/code&gt; then we proceed to check the validity of the Form. Here is what the SignupHandler looks like:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;func SignupHandler(w http.ResponseWriter, r *http.Request){&lt;br&gt;	c := appengine.NewContext(r)&lt;br&gt;	if r.Method == &#34;GET&#34; {&lt;br&gt;		s := Signup{}&lt;br&gt;		writeFormSignup(w, s)&lt;br&gt;	} else if r.Method == &#34;POST&#34;{&lt;br&gt;		s := Signup{&lt;br&gt;			Username: r.FormValue(&#34;username&#34;),&lt;br&gt;			Password: r.FormValue(&#34;password&#34;),&lt;br&gt;			Email: r.FormValue(&#34;email&#34;),&lt;br&gt;			Verify: r.FormValue(&#34;verify&#34;),&lt;br&gt;			ErrorUser: &#34;&#34;,&lt;br&gt;			ErrorPassword: &#34;&#34;,&lt;br&gt;			ErrorPasswordMatch: &#34;&#34;,&lt;br&gt;			ErrorEmail: &#34;&#34;,&lt;br&gt;		}&lt;br&gt;		// verify signup info.&lt;br&gt;		if !(tools.IsUsernameValid(s.Username) &amp;&amp; &lt;br&gt;			tools.IsPasswordValid(s.Password) &amp;&amp;&lt;br&gt;			s.Password == s.Verify) ||&lt;br&gt;			(len(s.Email) &gt; 0 &amp;&amp; !tools.IsEmailValid(s.Email)){&lt;br&gt;			&lt;br&gt;			if ! tools.IsUsernameValid(s.Username){&lt;br&gt;				s.ErrorUser = &#34;That&#39;s not a valid user name.&#34;&lt;br&gt;			}&lt;br&gt;			// more code for each input.&lt;br&gt;			// ...&lt;br&gt;			s.Password = &#34;&#34;&lt;br&gt;			s.Verify = &#34;&#34;&lt;br&gt;			writeFormSignup(w s)&lt;br&gt;			}&lt;br&gt;		}else{&lt;br&gt;			http.Redirect(w,r, &#34;/unit2/welcome?username=&#34;+s.Username, http.StatusFound)&lt;br&gt;		}&lt;br&gt;	}&lt;br&gt;}&lt;br&gt;&lt;/pre&gt;I put all the validation of inputs in a small &lt;b&gt;valid.go&lt;/b&gt; file with some helper functions. Right now I am adding this to a &lt;b&gt;Tools&lt;/b&gt; packages next to other helper functions. Though I might move it somewhere else later on.&lt;br&gt;The functions in &lt;b&gt;valid.go&lt;/b&gt; are the following:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;func IsUsernameValid(username string) bool&lt;br&gt;func IsPasswordValid(password string) bool&lt;br&gt;func IsEmailValid(email string) bool&lt;br&gt;func IsStringValid(s string) bool&lt;br&gt;&lt;/pre&gt;They work with the &lt;a href=&#34;http://golang.org/pkg/regexp/&#34;&gt;regexp&lt;/a&gt; package and check the validity in different ways. There is not much difference between the two implementations:&lt;br&gt;In Python:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;EMAIL_RE = re.compile(r&#34;^[\S]+@[\S]+\.[\S]+$&#34;)&lt;br&gt;def valid_email(email):&lt;br&gt;    return EMAIL_RE.match(email)&lt;/pre&gt;Here is the Go version:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;var EMAIL_RE = regexp.MustCompile(`^[\S]+@[\S]+\.[\S]+$`)&lt;br&gt;func IsEmailValid(email string) bool{&lt;br&gt;	return EMAIL_RE.MatchString(email)&lt;br&gt;}&lt;/pre&gt;In case of a wrong input, we update the signup data with the corresponding errors and execute again the signupTemplate.&lt;br&gt;In  case of a correct input we will redirect to the &lt;code&gt;/unit2/welcome&lt;/code&gt; page this time with a parameter username as follows:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;http.Redirect(w,r, &#34;/unit2/welcome?username=&#34;+s.Username, http.StatusFound)&lt;/pre&gt;&lt;/p&gt;&lt;br&gt;&lt;h4&gt;Unit 2: Welcome Handler&lt;/h4&gt;&lt;br&gt;&lt;p&gt;Once we redirect to the welcome handler we need to retrieve the information from the URL and display in. Here is the HTML of the welcome handler:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;&amp;lt;!DOCTYPE html&amp;gt;&amp;lt;html&amp;gt;&lt;br/&gt;&amp;lt;head&amp;gt;&lt;br/&gt;&amp;lt;title&amp;gt;Unit 2 Signup&amp;lt;/title&amp;gt;&lt;br/&gt;&amp;lt;/head&amp;gt;&lt;br/&gt;&amp;lt;body&amp;gt;&lt;br/&gt;&amp;lt;h2&amp;gt;Welcome, {{.}}!&amp;lt;/h2&amp;gt;&lt;br/&gt;&amp;lt;/body&amp;gt;&lt;br/&gt;&amp;lt;/html&amp;gt;&lt;/pre&gt;&lt;br&gt;In python we would get the user name value with a request.get method:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;class WelcomeHandler(webapp2.RequestHandler):&lt;br&gt;    def get(self):&lt;br&gt;        username= self.request.get(&#39;username&#39;)&lt;br&gt;        self.response.out.write(htmlWelcome%{&#39;username&#39;:username})&lt;br&gt;&lt;/pre&gt;In Go we do this by getting the information from the Form value method on the HTTP request:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;func WelcomeHandler(w http.ResponseWriter, r *http.Request){&lt;br&gt;	c := appengine.NewContext(r)&lt;br&gt;	if r.Method == &#34;GET&#34; {&lt;br&gt;		username := r.FormValue(&#34;username&#34;)&lt;br&gt;		if err := welcomeTemplate.Execute(w,username); err != nil{&lt;br&gt;			http.Error(w, err.Error(), http.StatusInternalServerError)&lt;br&gt;			return&lt;br&gt;		}&lt;br&gt;	}else{&lt;br&gt;		tools.Error404(w)&lt;br&gt;		return&lt;br&gt;	}&lt;br&gt;}&lt;br&gt;&lt;/pre&gt;&lt;/p&gt;&lt;br&gt;&lt;h4&gt;Organizing packages:&lt;/h4&gt;&lt;br&gt;&lt;p&gt;In the python version, I had a &lt;b&gt;unitx.py&lt;/b&gt; for each unit I had. In Go I have changed this by putting a Package for each unit. So right now I have 2 directories. &lt;b&gt;unit1&lt;/b&gt; and &lt;b&gt;unit2&lt;/b&gt;. What is interesting in Go is that I can have multiple &lt;b&gt;.go&lt;/b&gt; files and still have all of them correspond to the same package.&lt;br&gt;For example, in the unit2 package I have 3 files:&lt;br&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;rot13.go&lt;/li&gt;&lt;li&gt;signup.go&lt;/li&gt;&lt;li&gt;welcome.go&lt;/li&gt;&lt;/ul&gt;This separates the logic of each handler without the pain of importing each file separately. I think this is really nice.&lt;br&gt;This is also very nice when you are doing small helper functions. I did a &lt;b&gt;tools&lt;/b&gt; package and I have some files in it, like &lt;b&gt;valid.go&lt;/b&gt; which is used in this unit. And &lt;b&gt;server.go&lt;/b&gt; which handles 404 errors. It feels really nice to decouple each helper function into separate files and still have them all belong to the same package.&lt;/p&gt;&lt;p&gt;This covers the second CS253 Unit. Next time will be about Unit 3.&lt;/p&gt;&lt;small class=&#34;pull-right&#34;&gt;Santiaago&lt;/small&gt;</description>
      <link>https://www.sanarias.com/blog/613PortingUdacityCS253toGopart2/</link>
      <guid>https://www.sanarias.com/blog/613PortingUdacityCS253toGopart2/</guid>
      <pubDate>Tue, 25 Jun 2013 09:06:53 -0000</pubDate>
    </item>
    
    <item>
      <title>Porting Udacity CS253 to Go part1</title>
      <description>&lt;h3&gt;&lt;a href=&#34;/blog/613PortingUdacityCS253toGopart1&#34; class=&#34;post-link&#34;&gt;Porting Udacity CS253 to Go part1&lt;/a&gt;&lt;/h3&gt;
&lt;h4&gt;&lt;small&gt;Jun 12, 2013&lt;/small&gt;&lt;/h4&gt;
&lt;p&gt;I am starting to port &lt;a href=&#34;https://www.udacity.com&#34;&gt;Udacity&lt;/a&gt; course &lt;a href=&#34;https://www.udacity.com/course/cs253&#34;&gt;CS253 Web Development&lt;/a&gt; from python to &lt;a href=&#34;http://golang.org/&#34;&gt;Go&lt;/a&gt;. This seems to be a good way to get into Go while covering App Engine SDK.&lt;br&gt;&lt;br&gt;&lt;ul&gt;&lt;li&gt;The python &lt;a href=&#34;https://github.com/santiaago/udacity.cs253&#34;&gt;repository&lt;/a&gt;.&lt;/li&gt;&lt;li&gt;The Go &lt;a href=&#34;https://github.com/santiaago/udacity.cs253.go&#34;&gt;repository&lt;/a&gt;.&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;&lt;b&gt;Note:&lt;/b&gt;If you have any comments please do so, I will be more than happy to update and improve my code and learn to better develop in Go code.&lt;/p&gt;&lt;br&gt;&lt;h4&gt;Starting with Go and App Engine&lt;/h4&gt;&lt;br&gt;&lt;p&gt;To start with Go and App Engine there are a lot of resources out there. Here are the ones I used the most:&lt;br&gt;&lt;ul&gt;&lt;li&gt;The &lt;a href=&#34;http://tour.golang.org/&#34;&gt;Go Tour&lt;/a&gt;.&lt;/il&gt;&lt;li&gt;&lt;a href=&#34;http://pluralsight.com/training/Courses/TableOfContents/go&#34;&gt;PluralSight&lt;/a&gt; course on Go.&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;http://golang.org/doc/articles/wiki/&#34;&gt;Writing Web Applications&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href=&#34;https://www.udacity.com/course/cs253&#34;&gt;Udacity CS253 course&lt;/a&gt; (python and Google App Engine).&lt;/li&gt;&lt;li&gt;The Getting Started &lt;a href=&#34;https://developers.google.com/appengine/docs/go/gettingstarted/introduction&#34;&gt;tutorial&lt;/a&gt; of GAE in Go&lt;/li&gt;&lt;/ul&gt;&lt;/p&gt;&lt;br&gt;&lt;h4&gt;Installation and commands&lt;/h4&gt;&lt;br&gt;&lt;p&gt;Go App Engine SDK does not have a &lt;a href=&#34;https://developers.google.com/appengine/downloads&#34;&gt;GoogleAppEngineLauncher&lt;/a&gt; (like for python). So you will have to launch and deploy your application via the command line.&lt;br&gt;To run my application I do as follows: &lt;pre&gt;~/google_appengine/dev_appserver.py udacity.cs253.go/&lt;/pre&gt;I put the google_appengine sdk in &lt;code&gt;~&lt;/code&gt;. I had some troubles when I put the Go GAE scripts in my &lt;code&gt;PATH&lt;/code&gt; as I also have the python GAE SDK, I think it conflicts. This is the reason why I am explicit when running &lt;b&gt;dev_appserver.py&lt;/b&gt;.&lt;br&gt;To deploy my application I do as follows: &lt;pre&gt;~/google_appengine/appcfg.py update udacity.cs253.go&lt;/pre&gt;&lt;b&gt;Note:&lt;/b&gt; udacity.cs253.go is the name of my application.&lt;/p&gt;&lt;br&gt;&lt;h4&gt;Unit 1: Hello World and Handlers&lt;/h4&gt;&lt;br&gt;&lt;p&gt;So I started with Unit 1. Here is what the python code of my &lt;b&gt;main.py&lt;/b&gt; looks like in python, 3 handlers a simple &lt;i&gt;hello world&lt;/i&gt; handler and 2 handlers &lt;i&gt;date&lt;/i&gt; and &lt;i&gt;thanks&lt;/i&gt; that comunicate through a Form:&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;import webapp2&lt;br&gt;import re&lt;br&gt;from unit1 import *&lt;br&gt;class MainHandler(webapp2.RequestHandler):&lt;br&gt;    def get(self):&lt;br&gt;        self.response.out.write(&#39;Hello Udacity!&#39;)&lt;br&gt;app = webapp2.WSGIApplication([(&#39;/&#39;, MainHandler),&lt;br&gt;                                (&#39;/unit1/date&#39;,DateHandler),&lt;br&gt;                                (&#39;/unit1/thanks&#39;,ThanksHandler)],debug=True)&lt;br&gt;&lt;/pre&gt;And here is what my &lt;b&gt;main.go&lt;/b&gt; looks like after some work.&lt;br&gt;&lt;br&gt; &lt;pre  class=&#34;prettyprint&#34;&gt;package main&lt;br&gt;import (&lt;br&gt;	&#34;fmt&#34;&lt;br&gt;	&#34;net/http&#34;&lt;br&gt;	&#34;appengine&#34;&lt;br&gt;	&#34;unit1&#34;&lt;br&gt;)&lt;br&gt;func init(){&lt;br&gt;	http.HandleFunc(&#34;/&#34;, mainHandler)&lt;br&gt;	http.HandleFunc(&#34;/unit1/date&#34;, unit1.DateHandler)&lt;br&gt;	http.HandleFunc(&#34;/unit1/thanks&#34;, unit1.ThanksHandler)&lt;br&gt;}&lt;br&gt;func mainHandler(w http.ResponseWriter, r *http.Request){&lt;br&gt;	c := appengine.NewContext(r)&lt;br&gt;	c.Infof(&#34;Requested URL: %v&#34;, r.URL)&lt;br&gt;		fmt.Fprint(w,&#34;hello Udacity with Go!&#34;)&lt;br&gt;} &lt;br&gt;&lt;/pre&gt;I am using the context interface to &lt;a href=&#34;https://developers.google.com/appengine/docs/go/#Logging&#34;&gt;log&lt;/a&gt; in the console.&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;c.Infof(&#34;Requested URL: %v&#34;, r.URL)&lt;/pre&gt;This will display on your console as follows:&lt;br&gt;&lt;br&gt;&lt;pre&gt;2013/06/09 19:38:14 INFO: Requested URL: /&lt;/pre&gt;Also, I decided to do a package for every unit so that I can simply import each package like this:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;import (&lt;br&gt;	&#34;fmt&#34;&lt;br&gt;	&#34;net/http&#34;&lt;br&gt;	&#34;appengine&#34;&lt;br&gt;	&#34;unit1&#34;&lt;br&gt;	&#34;unit2&#34;&lt;br&gt;        &#34;unit3&#34;&lt;br&gt;)&lt;br&gt;&lt;/pre&gt;&lt;br&gt;I won&#39;t be externalizing this so there is no need to do the &lt;b&gt;github/santiaago/..&lt;/b&gt; organization for packages.&lt;br&gt;&lt;/p&gt;&lt;br&gt;&lt;h4&gt;Unit 1: Date and Thanks Handlers&lt;/h4&gt;&lt;br&gt;&lt;p&gt;The &lt;code&gt;/unit1/date&lt;/code&gt; url has a simple form with month, day and year inputs and a submit button.&lt;br&gt;In &lt;a href=&#34;http://jinja.pocoo.org/docs/templates/&#34;&gt;Jinja&lt;/a&gt; this is the template that was used for the course:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;&amp;lt;form method=&amp;quot;post&amp;quot;&amp;gt;&lt;br/&gt;   What is your birthday?&lt;br/&gt;   &amp;lt;br&amp;gt;&lt;br/&gt;	&amp;lt;label&amp;gt;Month&lt;br/&gt;   	&amp;lt;input name=&amp;quot;month&amp;quot; value=&amp;quot;%(month)s&amp;quot;&amp;gt;&lt;br/&gt;   &amp;lt;/label&amp;gt;&lt;br/&gt;	&amp;lt;label&amp;gt;Day&lt;br/&gt;		&amp;lt;input name=&amp;quot;day&amp;quot; value=&amp;quot;%(day)s&amp;quot;&amp;gt;&lt;br/&gt;   &amp;lt;/label&amp;gt;&lt;br/&gt;	&amp;lt;label&amp;gt;Year&lt;br/&gt;		&amp;lt;input name=&amp;quot;year&amp;quot; value=&amp;quot;%(year)s&amp;quot;&amp;gt;&lt;br/&gt;   &amp;lt;/label&amp;gt;&lt;br/&gt;	&amp;lt;div style=&amp;quot;color:red&amp;quot;&amp;gt;%(error)s&amp;lt;/div&amp;gt;&lt;br/&gt;	&amp;lt;br&amp;gt;&lt;br/&gt;   &amp;lt;br&amp;gt;&lt;br/&gt;   &amp;lt;input type=&amp;quot;submit&amp;quot;&amp;gt;&lt;br/&gt;&amp;lt;/form&amp;gt;&lt;/pre&gt;Go handles &lt;a href=&#34;http://golang.org/pkg/html/template/&#34;&gt;templates&lt;/a&gt; in the html package, so there is no need to install jinja or any template API, that is a great plus. Here is my resulting template for this part of the course.&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;&amp;lt;html&amp;gt;&lt;br/&gt;&amp;lt;body&amp;gt;&lt;br/&gt;&amp;lt;form method=&amp;quot;post&amp;quot;&amp;gt;&lt;br/&gt;   What is your birthday?&lt;br/&gt;   &amp;lt;br&amp;gt;&lt;br/&gt;	&amp;lt;label&amp;gt;Month&lt;br/&gt;   	&amp;lt;input name=&amp;quot;month&amp;quot; value=&amp;quot;{{.Month}}&amp;quot;&amp;gt;&lt;br/&gt;   &amp;lt;/label&amp;gt;&lt;br/&gt;	&amp;lt;label&amp;gt;Day&lt;br/&gt;		&amp;lt;input name=&amp;quot;day&amp;quot; value=&amp;quot;{{.Day}}&amp;quot;&amp;gt;&lt;br/&gt;   &amp;lt;/label&amp;gt;&lt;br/&gt;	&amp;lt;label&amp;gt;Year&lt;br/&gt;		&amp;lt;input name=&amp;quot;year&amp;quot; value=&amp;quot;{{.Year}}&amp;quot;&amp;gt;&lt;br/&gt;   &amp;lt;/label&amp;gt;&lt;br/&gt;	&amp;lt;div style=&amp;quot;color:red&amp;quot;&amp;gt;{{.Error}}&amp;lt;/div&amp;gt;&lt;br/&gt;	&amp;lt;br&amp;gt;&lt;br/&gt;   &amp;lt;br&amp;gt;&lt;br/&gt;   &amp;lt;input type=&amp;quot;submit&amp;quot;&amp;gt;&lt;br/&gt;&amp;lt;/form&amp;gt;&lt;br/&gt;&amp;lt;/body&amp;gt;&lt;br/&gt;&amp;lt;/html&amp;gt;&lt;/pre&gt;Some notes about this:&lt;br&gt;&lt;ul&gt;&lt;li&gt;Templates in Go work with curly braces.&lt;/li&gt;&lt;li&gt;Notice that I had to add the html, and body tags to this template else it would display an error as the html is not a valid web page.&lt;/li&gt;&lt;/ul&gt;This template works with its corresponding structure. I made a structure &lt;b&gt;Date&lt;/b&gt; for all the fields present in the html form:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;type Date struct{&lt;br&gt;	Month string&lt;br&gt;	Day string&lt;br&gt;	Year string&lt;br&gt;	Error string&lt;br&gt;}&lt;/pre&gt;To render the form I now do the following:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;func DateHandler(w http.ResponseWriter, r *http.Request){&lt;br&gt;	if r.Method == &#34;GET&#34; {&lt;br&gt;		date := Date{&lt;br&gt;			Month: &#34;&#34;,&lt;br&gt;			Day: &#34;&#34;,&lt;br&gt;			Year: &#34;&#34;,&lt;br&gt;		}&lt;br&gt;		if err := dateTemplate.Execute(w,date); err != nil{&lt;br&gt;			http.Error(w, err.Error(), http.StatusInternalServerError)&lt;br&gt;			return&lt;br&gt;		}&lt;br&gt;	} else if r.Method == &#34;POST&#34;{&lt;br&gt;                // ...&lt;br&gt;	}&lt;br&gt;}&lt;br&gt;&lt;/pre&gt; where &lt;b&gt;dateHTML&lt;/b&gt; is the html template defined above and  &lt;b&gt;dateTemplate&lt;/b&gt; is defined as follows and uses the &lt;b&gt;template&lt;/b&gt; package:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;var dateTemplate = template.Must(template.New(&#34;MyDate&#34;).Parse(dateHTML))&lt;br&gt;&lt;/pre&gt;&lt;/p&gt;&lt;br&gt;&lt;h4&gt;Post and Get methods&lt;/h4&gt;&lt;p&gt;To handle Post and Get methods in python you would have a class for each handler and a &lt;b&gt;Post&lt;/b&gt; and &lt;b&gt;Get&lt;/b&gt; method as follows:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;class DateHandler(webapp2.RequestHandler):&lt;br&gt;    def get(self):&lt;br&gt;        # to something&lt;br&gt;        &lt;br&gt;    def post(self):&lt;br&gt;        # do something else&lt;/pre&gt;&lt;br&gt;In Go you don&#39;t have classes and each handler is a function itself. Right now I am testing the http.Request &lt;a href=&#34;http://golang.org/src/pkg/net/http/request.go?h=http.Request#L72&#34;&gt;Method&lt;/a&gt; to differentiate each case:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;func DateHandler(w http.ResponseWriter, r *http.Request){&lt;br&gt;	c := appengine.NewContext(r)&lt;br&gt;	c.Infof(&#34;cs253: Requested URL: %v&#34;, r.URL)&lt;br&gt;	c.Infof(&#34;cs253: Http METHOD: %v&#34;,r.Method)&lt;br&gt;	if r.Method == &#34;GET&#34; {&lt;br&gt;                // do something&lt;br&gt;	} else if r.Method == &#34;POST&#34;{&lt;br&gt;                // do something else&lt;br&gt;	}else{&lt;br&gt;                // this is an error&lt;br&gt;	}&lt;br&gt;}&lt;/pre&gt;&lt;/p&gt;&lt;br&gt;&lt;h4&gt;Extracting information from the Form&lt;/h4&gt;&lt;br&gt;&lt;p&gt;On the post method we will extract the form information, this is a very common case.&lt;br&gt;In python you would get the information as follows:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;def post(self):&lt;br&gt;   user_month = self.request.get(&#39;month&#39;)&lt;br&gt;   user_day = self.request.get(&#39;day&#39;)&lt;br&gt;   user_year = self.request.get(&#39;year&#39;)&lt;br&gt;&lt;br&gt;   month = valid_month(user_month)&lt;br&gt;   day = valid_day(user_day)&lt;br&gt;   year = valid_year(user_year)&lt;br&gt;   if not(month and day and year):&lt;br&gt;       self.write_form(&#34;That&#39;s an error!&#34;,user_month,user_day,user_year)&lt;br&gt;   else:&lt;br&gt;       self.redirect(&#39;/unit1/thanks&#39;)&lt;/pre&gt;&lt;br&gt;In Go this is how I handled this:&lt;br&gt;&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;func DateHandler(w http.ResponseWriter, r *http.Request){&lt;br&gt;	c := appengine.NewContext(r)&lt;br&gt;	c.Infof(&#34;cs253: Requested URL: %v&#34;, r.URL)&lt;br&gt;	c.Infof(&#34;cs253: Http METHOD: %v&#34;,r.Method)&lt;br&gt;	if r.Method == &#34;GET&#34; {&lt;br&gt;                // the GET method&lt;br&gt;	} else if r.Method == &#34;POST&#34;{&lt;br&gt;		d := Date{&lt;br&gt;			Month: validMonth(r.FormValue(&#34;month&#34;)),&lt;br&gt;			Day: validDay(r.FormValue(&#34;day&#34;)),&lt;br&gt;			Year: validYear(r.FormValue(&#34;year&#34;)),&lt;br&gt;					}&lt;br&gt;		if d.Day == &#34;&#34; || d.Month == &#34;&#34; || d.Year == &#34;&#34;{&lt;br&gt;			d.Error = &#34;That&#39;s an error!&#34;&lt;br&gt;			if err := dateTemplate.Execute(w,d); err != nil{&lt;br&gt;				http.Error(w, err.Error(), http.StatusInternalServerError)&lt;br&gt;				return&lt;br&gt;			}&lt;br&gt;		}&lt;br&gt;		http.Redirect(w,r, &#34;/unit1/thanks&#34;, http.StatusFound)&lt;br&gt;	}&lt;br&gt;}&lt;br&gt;&lt;/pre&gt;&lt;br&gt;To get the values I use the &lt;a href=&#34;http://golang.org/src/pkg/net/http/request.go?h=FormValue#L789&#34;&gt;FormValue&lt;/a&gt; method on the http.Request. Then just execute the template with the structure.&lt;br&gt;This covers the first CS253 Unit. Next time will be about Unit 2.&lt;br&gt;&lt;small class=&#34;pull-right&#34;&gt;Santiaago&lt;/small&gt;&lt;/p&gt;</description>
      <link>https://www.sanarias.com/blog/613PortingUdacityCS253toGopart1/</link>
      <guid>https://www.sanarias.com/blog/613PortingUdacityCS253toGopart1/</guid>
      <pubDate>Wed, 12 Jun 2013 20:09:28 -0000</pubDate>
    </item>
    
    <item>
      <title>Learning Dart through pairing sessions</title>
      <description>&lt;h3&gt;&lt;a href=&#34;/blog/513LearningDartthroughpairingsessions&#34; class=&#34;post-link&#34;&gt;Learning Dart through pairing sessions&lt;/a&gt;&lt;/h3&gt;
&lt;h4&gt;&lt;small&gt;May 20, 2013&lt;/small&gt;&lt;/h4&gt;
&lt;p&gt;On May 2nd &lt;a href=&#34;https://plus.google.com/117928918793969810642&#34;&gt;Chris Strom&lt;/a&gt; wrote a &lt;a href=&#34;https://plus.google.com/u/0/117928918793969810642/posts/Wc4SgNDhQeW&#34;&gt;message&lt;/a&gt; on G+. He was looking for some candidates to try out the new Google Hangouts Remote Desktop while working on &lt;a href=&#34;http://www.dartlang.org/&#34;&gt;Dart&lt;/a&gt;.&lt;br&gt;I decided to give it a try, though I had never done any Dart development prior to this meeting.&lt;br&gt; &lt;blockquote&gt;&lt;p&gt;Zero Dart experience required. Really :)&lt;/p&gt;&lt;small&gt;Chris Strom&lt;/small&gt;&lt;/blockquote&gt;This quote gave me enough confidence to send him an email and try this out. Since then I have done more than 5 #pairwithme sessions with him while switching between his machine and mine.&lt;br&gt;&lt;br&gt;I want to use this post to review some of the things I have learned through these sessions. I believe more #pairwithme sessions should exist in any community.&lt;br&gt;&lt;br&gt;By doing pair reviews and working on each other&#39;s machines, you get to see how others would tackle certain problems, how they would read errors or debug stuff. This allows you to get smoothly into the language while avoiding silly mistakes. Also, you get to learn from each other&#39;s methods and that is always a plus.&lt;br&gt;&lt;/p&gt;&lt;br&gt;&lt;h4&gt;Getting started:&lt;/h4&gt;&lt;p&gt;To my surprise the only thing I needed to get started was the &lt;a href=&#34;http://www.dartlang.org/tools/&#34;&gt;Dart Editor&lt;/a&gt; and a fork of the &lt;a href=&#34;https://github.com/eee-c/ice-code-editor&#34;&gt;ice-code-editor&lt;/a&gt; (the JavaScript version is live &lt;a href=&#34;http://gamingjs.com/ice/&#34;&gt;here&lt;/a&gt; ).&lt;br&gt;The &lt;i&gt;out of the box&lt;/i&gt; experience with Dart is excellent. You are up and running in a short period of time and the &lt;a href=&#34;http://www.dartlang.org/docs/tutorials/&#34;&gt;Dart tutorials&lt;/a&gt; are a great material to get you started.&lt;/p&gt;&lt;br&gt;&lt;h4&gt;About Google Hangout remote desktop:&lt;/h4&gt;&lt;p&gt;The quality of the remote desktop is great. The latency between screen updates has been very low so the pairing experience has been very pleasant even though I am currenlty in France and Chris is in the U.S.&lt;br&gt;We did encounter some issues when using each others keyboards. I have an &lt;i&gt;azerty&lt;/i&gt; keyboard while Chris has a &lt;i&gt;qwerty&lt;/i&gt; one.&lt;/p&gt;&lt;br&gt;&lt;h4&gt;Emacs and Dart:&lt;/h4&gt;&lt;p&gt;There is a &lt;a href=&#34;https://github.com/nex3/dart-mode&#34;&gt;dart-mode&lt;/a&gt; for emacs. You can do a &lt;code&gt;M-x package-list-packages&lt;/code&gt;, search for &lt;code&gt;dart-mode&lt;/code&gt;, once you have found the package press &lt;code&gt;I&lt;/code&gt; to mark for installation, then press &lt;code&gt;X&lt;/code&gt; to start the package installation.&lt;/p&gt;&lt;br&gt;&lt;h4&gt;Dart paths and commands (OSX):&lt;/h4&gt;&lt;ul&gt;&lt;br&gt;  &lt;li&gt;&lt;code&gt;Chromium&lt;/code&gt;: is available under &lt;code&gt;dart/Chromium/Chromium.app&lt;/code&gt;. Is good to have Chromium running while you are working on your app.&lt;/li&gt;&lt;br&gt;  &lt;li&gt;&lt;code&gt;DartEditor&lt;/code&gt;: is available under &lt;code&gt;dart/DartEditor.app&lt;/code&gt;. The editor is really easy to work with and is well presented in the Dart Tutorials.&lt;/li&gt;&lt;br&gt;  &lt;li&gt;To run an application you can do &lt;code&gt;dart yourapp.dart&lt;/code&gt; from a command line.  The &lt;code&gt;dart&lt;/code&gt; command is available under &lt;code&gt;dart/dart-sdk/bin&lt;/code&gt; you might want to add that to your path.&lt;/li&gt;&lt;br&gt;  &lt;li&gt;The &lt;code&gt;dartanalyzer&lt;/code&gt; does the same work that the DartEditor does when it highlights errors and warning. It is also available under &lt;code&gt;dart/dart-sdk/bin&lt;/code&gt; and is great if you want to use other editors like vim or emacs.&lt;/li&gt;&lt;/ul&gt;&lt;br&gt;&lt;h4&gt;On Dart:&lt;/h4&gt;&lt;p&gt;Through this pair review sessions we have worked on multiple issues while porting to Dart the ice-code-editor. This is great because you get to see multiple aspects of the language (Unit tests, JS interop, DOM usage). Here are some of the things we went through during these sessions.&lt;br&gt;&lt;br&gt;&lt;b&gt;DOM manipulation:&lt;/b&gt;&lt;br&gt;We worked on &lt;a href=&#34;https://github.com/eee-c/ice-code-editor/commit/e84118c948d944997791773f8b5c4c46d5cd5b95&#34;&gt;hidding some JS dependencies&lt;/a&gt; from the ice-code-editor. It turns out that html manipulation is really smooth.&lt;br&gt;Take a look:&lt;br&gt;  &lt;pre class=&#34;prettyprint&#34;&gt;var script = new ScriptElement();&lt;br&gt;script.src = &#34;packages/ice_code_editor/ace/ace.js&#34;;&lt;br&gt;document.head.nodes.add(script);&lt;/pre&gt;&lt;b&gt;Javascript interoperability:&lt;/b&gt;&lt;br&gt;There are some examples of how to call Dart from JavaScript and how to handle callbacks &lt;a href=&#34;http://www.dartlang.org/articles/js-dart-interop/#calling-dart-from-javascript&#34;&gt;here&lt;/a&gt; and &lt;a href=&#34;http://www.dartlang.org/articles/js-dart-interop/#managing-callback-lifetimes&#34;&gt;here&lt;/a&gt;. We came up with a similar &lt;a href=&#34;https://github.com/eee-c/ice-code-editor/commit/e133dc10a9262c85bc882b431a0fadda95c2d8dc&#34;&gt;solution&lt;/a&gt;.&lt;br&gt;&lt;br&gt;&lt;b&gt;Keyboard events:&lt;/b&gt;&lt;br&gt;We also &lt;a href=&#34;https://github.com/eee-c/ice-code-editor/commit/79f18cd8c0df8de885d1fdef0875a760b0c9bd16&#34;&gt;worked&lt;/a&gt; on some &lt;a href=&#34;http://api.dartlang.org/docs/bleeding_edge/dart_html/KeyboardEvent.html&#34;&gt;Keyboard events&lt;/a&gt; in order to delay preview updates in the ice-code-editor.&lt;br&gt;&lt;br&gt;&lt;b&gt;Unit tests:&lt;/b&gt;&lt;br&gt;We have also tackled some &lt;a href=&#34;http://www.dartlang.org/articles/dart-unit-tests/&#34;&gt;unit tests&lt;/a&gt; in Dart. You know it is always good to have them :-),&lt;br&gt;&lt;/p&gt;&lt;br&gt;&lt;h4&gt;Misc:&lt;/h4&gt;&lt;p&gt;Through these sessions I also learned a lot of things that I didn&#39;t expect.&lt;br&gt;&lt;b&gt;Network emulation in linux:&lt;/b&gt;&lt;br&gt;You can emulate network latency &lt;a href=&#34;http://www.linuxfoundation.org/collaborate/workgroups/networking/netem&#34;&gt;netem&lt;/a&gt; as described &lt;a href=&#34;http://japhr.blogspot.nl/2011/07/network-emulation-in-linux.html&#34;&gt;here&lt;/a&gt;.&lt;br&gt;&lt;b&gt;Emacs commands and lisp expressions for my .emacs:&lt;/b&gt;&lt;br&gt;With the following code on your .emacs you can simply select a region and do a &lt;code&gt;C-x C-;&lt;/code&gt; to comment out a region of code.&lt;br&gt;&lt;pre class=&#34;prettyprint&#34;&gt;;;comment region&lt;br&gt;(global-set-key [?\C-x?\C-\;] &#39;comment-or-uncomment-region)&lt;/pre&gt;If you want to insert the content of a file into another file, you can perform a &lt;code&gt;C-x i&lt;/code&gt; and choose the file you want to insert. Then press &lt;code&gt;Enter&lt;/code&gt;.&lt;br&gt;&lt;br&gt;That&#39;s about it. Not bad after just a few sessions of Dart in my opinion. I hope someone finds it helpful, and maybe some of you might want to try out or suggest more pair review sessions through the G+ hangouts.&lt;br&gt;Finally &lt;b&gt;huge&lt;/b&gt; thanks to &lt;a href=&#34;https://plus.google.com/u/0/117928918793969810642&#34;&gt;Chris Strom&lt;/a&gt; for proposing these #pairwithme sessions they have been really helpful.&lt;/p&gt;&lt;small class=&#34;pull-right&#34;&gt;Santiaago&lt;/small&gt;</description>
      <link>https://www.sanarias.com/blog/513LearningDartthroughpairingsessions/</link>
      <guid>https://www.sanarias.com/blog/513LearningDartthroughpairingsessions/</guid>
      <pubDate>Mon, 20 May 2013 21:20:49 -0000</pubDate>
    </item>
    
    <item>
      <title>2012 in review</title>
      <description>&lt;h3&gt;&lt;a href=&#34;/blog/1132012inreview&#34; class=&#34;post-link&#34;&gt;2012 in review&lt;/a&gt;&lt;/h3&gt;
&lt;h4&gt;&lt;small&gt;Jan 07, 2013&lt;/small&gt;&lt;/h4&gt;
&lt;p&gt;2012 was a good year, during which I learned a lot of things. What is interesting is that it was mainly driven by two points: My desire to learn and the availability of online courses.&lt;br&gt;&lt;br&gt;I &lt;a href=&#34;/resume#moocs&#34;&gt;completed&lt;/a&gt; 7 online courses during 2012. Here are some of the concepts I improved while doing this.&lt;br&gt;&lt;br&gt;&lt;b&gt;Python&lt;/b&gt;:&lt;br&gt;&lt;br&gt;I used online courses as a way to really learn and improve my python knowledge and I can say that I am pleased with where I am right now (I know there is room for improvement, 2013 here I come). I have learned a lot by doing this. All &lt;a href=&#34;http://www.udacity.com&#34;&gt;Udacity&lt;/a&gt; courses helped me a lot to get into this language as they use it for all their courses. So, if you are new to python, Udacity courses are definitely a good place to start.&lt;br&gt;&lt;br&gt;&lt;b&gt;Artificial Intelligence&lt;/b&gt;&lt;br&gt;&lt;br&gt;I have done 3 courses in Artificial Intelligence, 2 from &lt;a href=&#34;http://www.udacity.com&#34;&gt;Udacity&lt;/a&gt; and one from &lt;a href=&#34;https://www.edx.org/&#34;&gt;Edx&lt;/a&gt;. I was familiar with some of the things taugth in the courses as I learned them while obtaining my Master&#39;s degree. It was fun to relearn them and learn more concepts from this field at the same time.&lt;br&gt;&lt;br&gt;My birthday present from 2011 was the AI book &lt;a href=&#34;http://www.google.com/search?q=artificial+intelligence+textbook&#34;&gt;Artificial Intelligence: A Modern Approach&lt;/a&gt;. It was really helpfull to go through these courses with the book by my side, as Udacity and Edx use it as a support for their courses. Definitely a good complement.&lt;br&gt;&lt;br&gt;I found that these 3 AI courses complement with each other if what you wish is to learn and master some main concepts from this field.&lt;br&gt;&lt;br&gt;&lt;b&gt;Web technologies&lt;/b&gt;&lt;br&gt;&lt;br&gt;I learned to use Google App Engine platform and API through &lt;a href=&#34;http://www.udacity.com/course/cs253&#34;&gt;CS253: Web Development&lt;/a&gt; and I am using it right now to host this site. &lt;a href=&#34;http://en.wikipedia.org/wiki/Steve_Huffman&#34;&gt;Steve Huffman&lt;/a&gt; is really a good teacher and in a small period of time you will have all the tools you need to build something on your own as so many students have done.&lt;br&gt;&lt;br&gt;The &lt;a href=&#34;https://class.coursera.org/saas/class/index&#34;&gt;SaaS&lt;/a&gt; course put my hands on a lot of different technologies that I would not have had time to discover on my own as fast as I did without the incentive of the course. This course was really fun and it took a lot of time and energy to achieve it, more than I expected, but the experience is definitely worthwhile.&lt;br&gt;I learned to use the Heroku platform, Amazon Web Services with EC2, as well as Ruby and other tools for testing web applications.&lt;br&gt;&lt;br&gt;&lt;br&gt;Online Courses can really be a good channel for you to learn or master concepts, the only thing you need is some discipline and being ready to have fun.&lt;br&gt;&lt;small class=&#34;pull-right&#34;&gt;Santiaago&lt;/small&gt;&lt;br&gt;&lt;/p&gt;</description>
      <link>https://www.sanarias.com/blog/1132012inreview/</link>
      <guid>https://www.sanarias.com/blog/1132012inreview/</guid>
      <pubDate>Mon, 07 Jan 2013 22:59:55 -0000</pubDate>
    </item>
    
  </channel>
</rss>