User:Soniakeys/Go/Automated Readability Index

Program description
For an example program that does something more meaningful than simply print Hello World to the screen, consider the Automated Readability Index, or ARI. ARI is one of a number of readability metrics in common use. It is different from most readability metrics in that it counts letters rather than syllables. Since letters are particularly easy to count, ARI makes for a succint example program. The formula is,
 * 4.71 (letters / word) + 0.5 (words / sentence) - 21.43

Complete source code
package main

import (   "fmt"    "unicode" )

func main { var inWord bool var nLetters, nWords, nSentences int for _, c := range text { switch { case unicode.IsLetter(c): if !inWord { inWord = true nWords++ }           nLetters++ case unicode.IsSpace(c): if inWord { inWord = false }       case c == '.': nSentences++ if inWord { inWord = false }       }    }    w := float(nWords) ari := 4.71*float(nLetters)/w + .5*w/float(nSentences) - 21.43 fmt.Println("ARI:", ari) }

var text string = `Four score and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty, and dedicated to the proposition that all men are created equal.

Now we are engaged in a great civil war, testing whether that nation, or any nation, so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this.

But, in a larger sense, we can not dedicate, we can not consecrate, we can not hallow this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us—that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion—that we here highly resolve that these dead shall not have died in vain—that this nation, under God, shall have a new birth of freedom—and that government of the people, by the people, for the people, shall not perish from the earth.`

The text used here is Abraham Lincoln's Gettysburg Address, and is traditional for demonstrating readability indexes.

Import declaration
import (   "fmt"    "unicode" ) Here we have two packages to import from the standard library. While we could use two import clauses, this parenthesized list format is often more convenient. Like the braces of function declarations, the open parenthesis must be on the same line as the keyword import. One design principle of Go is to reduce repition. The parenthesized list eliminates the need to repeat the keyword import. Especially for longer lists of packages, this minimizes typing and reduces visual clutter.

Variable declarations
Go variable declarations may feel backwards to C and C++ programmers. In Go, the variable name comes first, then the type. The variable name stays on the left even with complex type expressions. This keeps it nicely separated from the type expression and allows multiple variables to be declared with a single type expression.

var inWord bool var nLetters, nWords, nSentences int The keyword var declares one or more variables. Note that these declarations here are not top level, but are within the function main. The scope of the variable names then, is the function main. This means they can be accessed within main, but cannot be directly accessed by name outside of main.

These lines also introduce two new types. We saw the string type in Hello World. Here are the bool and int types. A bool can have a value of true or false. The int type here is a signed integer and has implementation dependent width, either 32 or 64 bits.

When variables are declared like this, they are automatically initialized to their zero values. All types have a zero value. The zero value for bool is false and the zero value for int is 0. That may seem overly pedantic, but it is important in many cases to know exactly the initial value of a variable. The compiler guarantees that variables are initialized to their zero values and you can rely on this in your code.

Assignment statement
inWord = true

Short declaration
w := float(nWords) Same as   var w float = float(nWords)

For statement with range keyword
for _, c := range text { For is a loop construct, as in many programming languages. It has a few different forms in Go. In this form, the range keyword directs iteration over the characters in the the string text. Successive characters are assigned to the variable c.

Switch statement
switch { case unicode.IsLetter(c):

If statement
if !inWord {

Increment statement
nWords++

Type conversion
w := float(nWords)

Package scope
var text string =

Raw string literal
`Four score and seven years ago our fathers brought forth on this continent