Go Challenge - 5

update.jpg

Winners have been declared

Winner #1 - Robert Horvath

Winner #2 - Fatih Arslan


The August 2015 Go Challenge for developers (newbies included)

Josh Bleecher Snyder: Author of the 5th Go Challenge

Josh Bleecher Snyder The 5th Go Challenge author is Josh Bleecher Snyder an active contributor to the Go project. He’s an engineer at Braintree and previously co-founded card.io.

Josh has this to say about the challenge:

“I love writing tools to work with Go. It gives you a much deeper appreciation for the language and places incredible (and delightfully dangerous) power at your fingertips.”


The Go Challenge 5

unexport
Preamble

Go 1.5rc1 is out! The gc compiler is now written in Go, and the go/types package is now part of the standard library.

Time for some fun. :)

The compiler was converted from C to Go mechanically. This helped avoid bugs, but it means that the code is still very C-ish. The Go team has been slowly and carefully doing cleanup to make the compiler more idiomatic. It is preferable to use type-safe tools (such as eg and gorename) to do large-scale cleanup, because they make fewer mistakes than humans.

But sometimes, the tool you need doesn’t exist. Fortunately, the standard library gives us amazing tools to build tools. So you build one.

Let’s build one.

Goals of the challenge

At a high level, the goal of this challenge is to write a tool that will find unnecessarily exported identifiers in a package and help unexport them.

This is inspired by the fact that the cmd/compile/internal/gc exports much more than it needs to. (This is ok insofar as it is an internal package, but it’d be better for the API to be more precise.)

Requirements of the challenge

Solutions should:

  • Be general purpose. That is, the tool should accept a package as an argument.
  • Cover all exported identifiers, including functions, constants, variables, types, field names in structs, etc. If godoc shows it, it qualifies.
  • Allow the user to decide which identifiers should be unexported. There may be good reasons to keep an identified exported even if it is not used by any packages. One simple way to do this is to have unexport generate a series of gorename commands. The user could then decide which renamings to apply. It is up to you to design the interface as you please.
  • Use only packages found in the standard library and the golang.org/x/tools repo. Note that there are two copies of the go/types package, one in the standard library and one in golang.org/x/tools. It is ok to use either one. All else being equal, the standard library version is preferable, but the useful go/loader package in golang.org/x/tools is not (yet?) part of the standard library.
  • Use Go 1.5.

Bonus features:

  • Provide flag (-safe?) that restricts the use of unexport to internal packages. Only with an internal package can you know definitively that no one is using an exported identifier.
  • Provide a mode that shows the number of uses of each exported identifier, and perhaps by which packages they are used.
  • Warn if unexporting an identifier will cause a naming collision.
  • Whatever else you can think of that would be useful in such a tool. However, please keep the tool focused. If you think building such tools is fun and want ideas for other tools that would be helpful, just ask. :)
  • Concise, tone-neutral notes about places where you got stuck or confused and about places where the documentation or examples could be improved. This will help all future gophers. Super bonus points for writing blog posts or making other introductory materials to help others.

Hints

unexport might sound complicated to build. It’s actually not, thanks to the amazing packages you can build on. The hard part is getting up to speed on the tools at your disposal; the learning curve can be steep. Be patient, build incrementally, and make toys as you go.

In addition to the packages mentioned so far, you might also find the standard library’s go/* packages useful, particularly go/ast.

Examples of programs that use go/ast, go/types, and related packages include:

I recommend using little test packages during development rather than tackling cmd/compile/internal/gc, because package gc is large and complex.

Special note for the evaluators

Good entries will be small and obviously correct.

My personal goal in setting this challenge is to inspire more gophers to know about these tools and to solve programming/refactoring problems they face by writing tools. The Go team has laid the groundwork for an ecosystem of awesome tools and seeded it with powerful programs. I hope that this will encourage its growth.


Challenge Rules and how to enter the Go Challenge?

By participating in this challenge, you agree to be bound by the Challenge Rules below:

  • The Challenge is open to individuals.
  • Evaluators cannot enter the challenge except under the “Just for Fun” category.
  • Each entrant shall indemnify, defend, and hold JoshSoftware Pvt. Ltd. (who has sponsored the domain and is the organizer of these challenges) harmless from any third party claims arising from or related to that entrant’s participation in the Challenge. In no event shall JoshSoftware Pvt. Ltd. be liable to an entrant for acts or omissions arising out of or related to the Challenge or that entrant’s participation in the Challenge.
  • Odds of winning depend on the number and quality of entries received.
  • All taxes, including income taxes, are the sole responsibility of the winners.
  • No prize substitution is permitted.
  • Create a zip of your Go source code and test cases and send the zip file to golangchallenge [at] gmail.com before 25th of August 2015 (6 am IST). Use this link to find the equivalent time in your city/country. No new solutions will be accepted after that. In the email mention **your full name, country of residence, twitter or GitHub id (if any) and participating under which category - Just participating Participating and exploring further Just for Fun Anonymous entry**. We are accepting anonymous submissions and will evaluate them too but then these participants are not eligible for the prizes.
  • We will give your zip file to the evaluation team.
  • We shall be publishing on this blog, a list of participant names. If you don’t want your name to appear kindly mention the same in your email.
  • You are allowed to re-submit your code if you feel it is necessary.
  • Note: Avoid sharing your code with anyone else; if your solution becomes available to the general public it might impact evaluation of your submission.
  • After the challenge is over, all submissions will be made available online on GitHub under the BSD 3-Clause License or the GNU General Public License, version 3 - GPL-3.0 unless a participant has indicated that his/her solution should not be made public before the challenge ends.

How will the challenge be evaluated?

Entries will be anonymized and evaluated by the challenge author and a team of evaluators.

  • Functioning code and a test suite that passes.
  • Code hygiene. Use gofmt, vet and lint. Review CodeReviewComments.
  • Readability. How easy is it for another programmer to grasp what your entry is doing?
  • Code structure. Do types and files have good names?
  • Reliability. Are errors properly handled?
  • Appropriate consideration given to memory and performance (nothing is unnecessarily expensive).

Questions?

If you have any questions about this challenge, please join the golang-challenge channel on slack and ask your questions with the tag @josharian so that the challenge author is aware of your question(s) and can reply to the same (the author is totally unavailable from midday Fri 31st July until Monday morning 3rd Aug, Pacific time. Other than that, he will be available). This is a room for people who are going to participate in the Go Challenge. You can also send us an email at golangchallenge [at] gmail.com

Evaluators

Nathan Youngman had set the guidelines for evaluation for the first Go Challenge. Subsequently Dominik Honnef modified the guidelines based on his experience as an evaluator for the first challenge. Austin Riendeau, Cory LaNou, Edd Robinson, Gautam Dey, Jyotiska NK, Kevin Gillette and Pravin Mishra have agreed to go through all the submitted solutions of a challenge. They will comment and rank these solutions.

Best Solution

The author of the Go Challenge will decide the best five solutions. The author shall have the sole authority and discretion to select the award recipients.

Notification

The winning entries will be announced here on this blog. The winners will be sent their prizes by email/postal mail.

Prizes

The author of the challenge will select 5 winners but prizes will be awarded to the top 2 winners.

Here are some great prizes provided by our sponsors for the event.

Winner 1:

Winner 2:

Winner Interviews

After a winner wins the monthly challenge, he/she would be interviewed by Sourcegraph and the interview will be published on their blog.

Challenge Solutions

All the solutions submitted by the participants will be available here after the challenge ends.

The Winners

winner.png

Robert Horvath

Winner #1 - Robert Horvath is a software developer from Austria, currently working at Catalysts while pursuing a master’s degree in computer science at the Vienna University of Technology. He started programming at an early age and has been writing code ever since. Over the years he used many different programming languages, most importantly PHP, Java, C, Ruby and JavaScript. He only recently started dabbling in Go, but likes it and plans on using it for more projects soon.

His solution.


Fatih Arslan

Winner #2 - Fatih Arslan is a software engineer at Koding.com. He is working on the backend team and is responsible for managing and scaling user VMs. He is the creator of vim-go, the most popular editor plugin in Go. He also wrote several widely used Go packages, such as structs, color, kite, tunnel, pool, set, etc.. Check his GitHub id. He is an avid Go developer and has contributed to several open source projects.

His solution.


Sponsors

This challenge’s sponsors are: Anand D N, Cube Root Software, Docker, GitHub, InfluxDB, JoshSoftware Pvt. Ltd., Manning Publications Co., Packt Publishing, Qwinix Technologies, SoStronk and Sourcegraph.

Credit

  • The Gopher character is based on the Go mascot designed by Renée French and copyrighted under the Creative Commons Attribution 3.0 license.
  • GitHub for the yearly sponsorship of a GitHub Bronze Organisation plan for the Go Challenge.
  • The Go Challenge is being organized by JoshSoftware Pvt. Ltd. with help from the Go community.
Written on August 1, 2015