Fun with Go

Posted on November 14th, 2009 in General | No Comments »

So I spent some time on Go and there was one frustrating thing I found. I could not make an http request and specify headers with the simple http client. I finally looked at the package source for the http package and luckily it was so simple that I was able to create my own Get method that allowed headers. Basically all this code does is pull your friend status activity from twitter. You pass your twitter user and pass at the command line and it dumps the xml from twitter api. Next I will add formatted output and the ability to post your status to twitter. This was just an exercise for fun so I’m not sure how much more I’ll put into it. I think I’ll try it in python next.

I was able to play with many features of the language but I didn’t get to threading yet. I have command line parsing, string encoding to base 4 and simple http client with basic authentication headers. Here is the code including the modification to the http.Get function which turned out to be trivial. Let me know if you want the full source code.

usage:
twittergo -user username -pass password

package main

import (”os”;
“fmt”;
“flag”;
“http”;
“io”;
“encoding/base64″;
“strings”;
“bytes”;
“net”;
“bufio”;
“strconv”;
)

func main()
{
var user string;
var pass string;

flag.StringVar(&user, “user”, “username”, “twitter user name”);
flag.StringVar(&pass, “pass”, “password”, “twitter password”);

flag.Parse();

userpass := user + “:” + pass;

var userPassEnc []byte;
buffer := bytes.NewBuffer(userPassEnc);
enc := base64.NewEncoder(base64.StdEncoding, buffer);

enc.Write(strings.Bytes(userpass));
enc.Close();

fmt.Printf(”user: “);
fmt.Printf(user);
fmt.Printf(”\npass: “);
fmt.Printf(pass);
fmt.Printf(”\nuserpass: “);
fmt.Printf(userpass);
fmt.Printf(”\nuserpassenc: “);
fmt.Printf(buffer.String());
fmt.Printf(”\n”);

headers := map[string]string{
“Host” : “twitter.com”,
“Authorization”: “Basic ” + buffer.String(),
};

var r *http.Response;
r,url,error := GetWithHeaders(”http://twitter.com/statuses/friends_timeline.xml”, headers);
fmt.Printf(url);
bytes,error :=io.ReadAll(r.Body);
r.Body.Close();

os.Stdout.WriteString(”response: “);
os.Stdout.Write(bytes);
os.Stdout.WriteString(”\n”);

if (error != nil)
{
os.Stdout.WriteString(”error: “);
os.Stdout.WriteString(error.String());
os.Stdout.WriteString(”\n”);
}
}

// Get issues a GET to the specified URL. If the response is one of the following
// redirect codes, it follows the redirect, up to a maximum of 10 redirects:
//
// 301 (Moved Permanently)
// 302 (Found)
// 303 (See Other)
// 307 (Temporary Redirect)
//
// finalURL is the URL from which the response was fetched — identical to the input
// URL unless redirects were followed.
//
// Caller should close r.Body when done reading it.
func GetWithHeaders(url string, headers map[string]string) (r *http.Response, finalURL string, err os.Error) {
// TODO: if/when we add cookie support, the redirected request shouldn’t
// necessarily supply the same cookies as the original.
// TODO: set referrer header on redirects.
for redirect := 0; ; redirect++ {
if redirect >= 10 {
err = os.ErrorString(”stopped after 10 redirects”);
break;
}

var req http.Request;
req.Header = headers;
if req.URL, err = http.ParseURL(url); err != nil {
break
}
if r, err = send(&req); err != nil {
break
}
if shouldRedirect(r.StatusCode) {
r.Body.Close();
if url = r.GetHeader(”Location”); url == “” {
err = os.ErrorString(fmt.Sprintf(”%d response missing Location header”, r.StatusCode));
break;
}
continue;
}
finalURL = url;
return;
}

err = &http.URLError{”Get”, url, err};
return;
}

Google go is weird

Posted on November 13th, 2009 in General | No Comments »

I find it really strange that google created a modern language and left in some of the dangerous and confusing stuff from the C world. At first I was really put off by the use of pointers but at least no pointer arithmetic. As I read on through the examples and the language specification I started warming up to it. The thing I realized is that we have pointers in java and c# we just call them references. With built in memory management and garbage collection and cool shortcuts for inferring variable types and interface implementation, I am curious to see what this thing can do. I became really interested when I got to the part about goroutines even though I hate the name. It is cool that it requires so few lines of code to get things running in multiple threads and the synchronization is built in with channels. I am really just trying this out of curiosity although I’ve purposely avoided python and perl and other languages because I wanted to focus on others like ruby, php, javascript and c#. I am getting rabbit holed in c# lately so it’s good to get new perspective. I think I’ll even look at python next. I have to get my ubuntu linux partition back up, I haven’t used it since installing win 7 and it I wiped out the boot loader. I haven’t been brave enough to fix it since this is my primary work machine but I’m ready now. First thing tomorrow…

another interview link

Posted on March 13th, 2008 in General | No Comments »

http://www.gowrikumar.com/interview/index.html

recommended algorithm books

Posted on March 13th, 2008 in General | No Comments »

Recommended by the previous post

Introduction to Algorithms (Hardcover)
http://www.amazon.com/gp/product/0262032937?ie=UTF8&tag=steveysblogra-20&link_code=wql&camp=212361&creative=380601

The Algorithm Design Manual (Hardcover)
http://www.amazon.com/gp/product/0387948600?ie=UTF8&tag=steveysblogra-20&link_code=wql&camp=212361&creative=380601

good interview prep link

Posted on March 13th, 2008 in General | No Comments »

Good link sent by a co-worker:

http://steve-yegge.blogspot.com/2008/03/get-that-job-at-google.html

interview prep

Posted on January 10th, 2008 in General | No Comments »

I’ve been working as a contractor to Microsoft for over four months now and am about to take on the interview process for a full time position. I feel confident but at the same time I’m not too sure how I’ll feel on interview day.

I decided to do as much prep and research as possible before my interview. The first thing I did was to search the internet for stuff like data structures and algorithms that I wanted to memorize.

Next I looked for books on data structures and algorithms and I ran into a book on preparing for a programming interview. I bought the book and started reading. Link below.

I wanted to talk to other devs to find out what their interviews were like. I have no friends that work at Microsoft so I called up my recruiter who is awesome and got me the current contract position I’m in (let me know if you want his contact info). He set up meetings for two other programmers who had gone full time at MS after contracting through him. This was extremely helpful and I am grateful for their advice.

One of the guys recommended the programming interview book although I already had it and had read 1/3 of the way : )

I’ve noticed that many devs don’t make their first FTE interview. They say the main thing is to be relaxed and ask lots of questions. Other things are:

-be relaxed - repeated because it seems really important
-ask lots of questions - repeated because it seems really important
-don’t worry about weird questions like ‘why is a manhole cover round’
-focus on technology, team work, personality
-if you like the interviewer there’s a good chance they like you
-talk as you work out problems so they can see your thought process
-display a passion for the position or technology you’re interviewing for
-if you are confident of an answer then also elaborate if you know more about the topic
-if a question is hard or you are unsure at all, ask for more details
-if the interviewer runs out of questions, ask them questions about their job
-know why you want the job, you will be asked
-be confident about your skills
-don’t fake anything you don’t know (it’s ok not to know stuff)
-don’t answer quick, look for more than one way to answer and talk about multiple answers
-ask for conditions and background before trying to solve problems
-look for boundary or edge case problems in your solutions
-relax
-be honest
-search the internet for interview brain dumps

some tricky tech questions they were asked

-binary math questions - it’s ok if you can’t answer all just talk through it
-parse and convert roman numerals to decimal equivalent
-validate binary search tree
-implement compression schemes

The first link is a book that has really good info and really gets down to the fundamentals of programming in terms of interview questions. Data structures, algorithms, array manipulation and string manipulation seem to be the key items along with threading, recursion and OOP.

http://www.amazon.com/Programming-Interviews-Exposed-Secrets-Programmer/dp/047012167X/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1199940602&sr=1-1

http://channel9.msdn.com/ShowPost.aspx?PostID=18472
http://channel9.msdn.com/ShowPost.aspx?PostID=18718
http://channel9.msdn.com/ShowPost.aspx?PostID=19171

http://discuss.fogcreek.com/joelonsoftware/default.asp?cmd=show&ixPost=116513

http://www.sellsbrothers.com/fun/msiview/#Some_of_the_Interview_Process_Filmed


http://weblogs.asp.net/emaino/archive/2005/02/20/376865.aspx

http://www.hanselman.com/blog/WhatGreatNETDevelopersOughtToKnowMoreNETInterviewQuestions.aspx

http://members.microsoft.com/careers/mslife/insidetrack/resume.mspx

http://blogs.msdn.com/mswanson/archive/2004/08/21/218216.aspx

Another good link from a co-worker:
http://www.careercup.com/show/?co=a767af1f-f0cd-4149-bbc9-b7b73d5f84d1

here goes nothing

Posted on February 24th, 2007 in General | No Comments »

Getting started is always the hardest part! Don’t be so afraid of looking stupid, just get started and do something. It’s easier to make something that’s not up to par better than it is to make something out of nothing. Along the same lines, speak up and be heard and again don’t be afraid of looking stupid. If you have something to say then say it, everyone else is probably thinking the same thing and if they’re not then starting a discussion is the best thing you can do to get everyone on the same page. Don’t expect someone who’s in charge to make the right decisions, be heard and make sure you’re ideas are considered. Now get to work and don’t complain if you get knocked around a bit, it’s good for your character you’ll only benefit from it.

Let’s get messy!