Beginning Programming Lesson 08

Practical Example

Now that we have the basics down, let apply them to something more real, and perhaps the previous concepts covered will become more clear. When I learn a new programming language I generally start with trying to create a program to solve the Tricky Triangle. If you have ever been to Cracker Barrel, you know what it is. It goes by some other names too, but it is usually a triangular piece of wood with holes in it with enough golf tees to fill all but one whole. [Read More]

Beginning Programming Lesson 07

Packages and Modules

In previous lesson we put all of out code in one file and one package, main. However, aside from very small projects, this won’t scale very well, and does make your code very reusable apart from copying and pasting. We have already seen the usefulness of packages through using the fmt and errors packages. So let’s look at how to create our own. You can find the code for today’s lesson here. [Read More]

Beginning Programming Lesson 06

Recursion and Error Handling

When I was in college my professor explained recursion as “Pretend you already have a function that does what you need to do.” The problem with that explanation is, it only makes sense after you understand recursion. I think we can do better here. While we are at it, let’s look at error handling too. You can find the code for today’s lesson here. package main import ( "fmt" "log" ) type MinInputError struct { } func (e *MinInputError) Error() string { return "invalid input, number must be greater than 0" } type MaxInputError struct { } func (e *MaxInputError) Error() string { return "invalid input, number must be less than 100" } func Factorial(n int) int { if n == 1 { return n } return n * Factorial(n-1) } func Factorial2(n int) (int, error) { if n < 0 { return 0, &MinInputError{} } if n > 100 { return 0, &MaxInputError{} } ret := n for i := n - 1; i > 0; i-- { ret = ret * i } return ret, nil } func main() { fmt. [Read More]

Beginning Programming Lesson 05

Inheritance, Interfaces, and Type Assertion

We created complex data types (structs/classes), but what if we wanted to create another struct that expands upon another? Or you wanted to create a number of different structs that play by a certain set of rules. The example code for today can be found here. package main import ( "fmt" "math" ) type Polygon interface { Shape() string Perimeter() float64 Area() float64 } type Rectangle struct { Length float64 Height float64 } func (r Rectangle) Perimeter() float64 { return 2*r. [Read More]

Beginning Programming Lesson 04

Control Structures and Array-Like Things

We now know how to create and use variables, structs, and functions. But thus far, all the examples have been fairly linear. What if we want do things only when certain criteria are met? Or what if we want to do the same thing many times in a row? What if we want want store a bunch of a rectangles from the previous lesson? This is where control structures, arrays and slices come in handy. [Read More]

Beginning Programming Lesson 03

Complex Data Types (Structs/Classes)

In the last installment of this series we learned about variables, and covered some simple variable types. Now we are going to learn about complex data types. In short, they store more than a single data point of information. For example, what if we wanted to have a variable that stored information about a rectangle? There is more than one bit of information to describe it. At minimum you would probably want to store its width and length. [Read More]

Beginning Programming Lesson 02

Variables, Functions, Importing, Pointers, and Scope.

Now that you have your setup ready to go, we can start writing some code! Note that we are just touching the surface of the topics covered in this lesson, but the sooner you start writing code, the easier it will become to understand the topics better. Plus you are more likely to continue, if you feel like you are accomplishing something, rather than learning more and more theory. You can download the file here. [Read More]

Beginning Programming Lesson 01

Introduction and the Tools

When I was about 12 years old, my aunt gave us an old computer. Without going into all the details, I broke it, and all that it would then let you do is write programs in BASIC. It came with a book explaining how to do various things with the language. So having nothing better to do, I worked through it and taught myself how to code, and eventually went to college to learn how to do it better. [Read More]

Introduction to the Command Line

The basics

The command line can feel a bit like a step backwards in terms of user experience, or even intimidating. However, it can be a very powerful tool, and even impossible to avoid if you want to program or be involved in IT. What is it? Probably the most recognizable image of the command line is that black box on the screen with green text. Basically it is a place to interact with your computer via text rather than photos (mostly). [Read More]