Recent Updates RSS Toggle Comment Threads | Keyboard Shortcuts

  • Kevin 10:33 pm on August 5, 2010 Permalink | Reply
    Tags:   

    How To Use Arrays and Slices in Google Go 

    Array
    An array is a collection of like objects. In Google Go programming, arrays are declared as,

    var arrayOfInt [10]int
    

    The length is part of the array’s type and must be a constant expression that evaluates to a non-negative integer value. The length of array a can be discovered using the built-in function len(a). The elements can be indexed by integer indices 0 through the len(a)-1. If the array is indexed beyond len(a)-1, then we get an out of bounds error.

    In C, arrayOfInt would be usable as a pointer to int. In Go, since arrays are values, we have to use a pointer to an array. What this means is that in Google Go,

    var pArray *int = &arrayOfInt //pArray is a pointer to an integer.
    var pArray *[10]int = &arrayOfInt //pArray is a pointer to an array of 10 integers.
    

    In the declaration of the pointer to an array, the size of the array is mandatory. When assigning the address of an array to a pointer variable, the & referencing is mandatory.

    Arrays are values. Assigning one array to another copies all the elements. In particular, if you pass an array to a function, it will receive a copy of the array, not a pointer to it. The value property can be useful but also expensive; if you want C-like behavior and efficiency, you can pass a pointer to the array.

    package main
    
    import "fmt"
    
    func arrayLen (pArray *[5]int) int{
       return len(pArray)
    }
    
    func main(){
       var arrayOfInt [5]int
    
       arrayOfInt[0] = 0
       arrayOfInt[1] = 1
       arrayOfInt[2] = 2
       arrayOfInt[3] = 3
       arrayOfInt[4] = 4
    
       fmt.Println("Array Length = ", arrayLen(&arrayOfInt))
    }
    

    If you are creating a regular array but want the compiler to count the elements for you, use … as the array size:

    s := sum(&[...]int{1,2,3})
    

    A type followed by a brace-bounded expression—is a constructor for a value, in this case an array of 3 ints.

    Slice
    Slices wrap arrays to give a more general, powerful, and convenient interface to sequences of data. Slices can be considered as a sub-section of an array. Slices are reference types, which means that if you assign one slice to another, both refer to the same underlying array. For instance, if a function takes a slice argument, changes it makes to the elements of the slice will be visible to the caller, analogous to passing a pointer to the underlying array.

    We can declare a slice variable, by assigning a pointer to any array with the same element type,

    var arrayOfInt [5]int
    var slice []int = &arrayOfInt
    

    or by a slice expression of the form a[low : high], representing the subarray indexed by low through high-1.

    var arrayOfInt [5]int
    slice = arrayOfInt[1:3]
    

    Slices look a lot like arrays but have no explicit size ([] vs. [10]) and they reference a segment of an underlying, often anonymous, regular array. Multiple slices can share data if they represent pieces of the same array; multiple arrays can never share data.

    When passing an array to a function, you almost always want to declare the formal parameter to be a slice. When you call the function, take the address of the array and Go will create (efficiently) a slice reference and pass that.

    Using slices one can write the earlier program as:

    package main
    
    import "fmt"
    
    func arrayLen (slice []int) int{
       return len(slice)
    }
    
    func main(){
       var arrayOfInt [5]int
    
       arrayOfInt[0] = 0
       arrayOfInt[1] = 1
       arrayOfInt[2] = 2
       arrayOfInt[3] = 3
       arrayOfInt[4] = 4
    
       fmt.Println("Array Length = ", arrayLen(&arrayOfInt))
    
    }
    

    We pass the pointer to arrayLen() by (implicitly) promoting it to a slice.

    Like arrays, slices are indexable and have a length. The length of a slice s can be discovered by the built-in function len(s); unlike with arrays it may change during execution. The elements can be addressed by integer indices 0 through len(s)-1. The slice index of a given element may be less than the index of the same element in the underlying array.

     
  • Kevin 10:44 pm on August 2, 2010 Permalink | Reply
    Tags:   

    How To Define Functions in Google Go 

    A program can be broken down into modules that each perform a specific function. These modules interact with each other to perform the entire functionality of the program. Functions are the basis of modular programming in Google Go.

    In Google Go, a function is declared using the keyword func as shown below,

    func [function name] ([param1 declaration], [param2 declaration],...) (ret1 declaration, ret2 declaration, ...){
         //body of the function
    }

    The function arguments declaration (param1) and return values declaration (ret1) is similar to variable declaration except the var keyword is not used. (See How To Define Variables In Google Go)

    func min(x int, y int) int {
            if x < y {
                 return x
            }
            return y
    }

    In Google Go, it is necessary that the opening brace { of the scope should be placed on the same line as the function declaration else you will get a compilation error.

    func main()
    {                                          //Wrong
       fmt.Println("Hello World")
    }
    
    func main() {                         //Correct
       fmt.Println("Hello World")
    }

    One of Go’s unusual features is that functions and methods can return multiple values. Multiple return values are declared after the argument list and separated by a comma as shown below.

    package main
    
    import "fmt"
    
    func calc(a int, b int) (add int, sub int){
         add = a + b
         sub = a - b
         return
    }
    
    func main(){
         a := 100
         b := 50
         add, sub := calc(a, b)
         fmt.Println("Addition = ", add);
         fmt.Println("Subtraction = ", sub);
    }

    The return or result “parameters” of a Go function can be given names and used as regular variables, just like the incoming parameters. When named, they are initialized to the zero values for their types when the function begins; if the function executes a return statement with no arguments, the current values of the result parameters are used as the returned values.

    Defer
    Go’s defer statement schedules a function call to be run immediately before the function executing the defer returns. It’s an unusual but effective way to deal with situations such as resources that must be released regardless of which path a function takes to return. The canonical examples are unlocking a mutex or closing a file.

    // Contents returns the file's contents as a string.
    func Contents(filename string) (string, os.Error) {
        f, err := os.Open(filename, os.O_RDONLY, 0)
        if err != nil {
            return "", err
        }
        defer f.Close()  // f.Close will run when we're finished.
    
        var result []byte
        buf := make([]byte, 100)
        for {
            n, err := f.Read(buf[0:])
            result = bytes.Add(result, buf[0:n])
            if err != nil {
                if err == os.EOF {
                    break
                }
                return "", err  // f will be closed if we return here.
            }
        }
        return string(result), nil // f will be closed if we return here.
    }

    Deferring a function like this has two advantages. First, it guarantees that you will never forget to close the file, a mistake that’s easy to make if you later edit the function to add a new return path. Second, it means that the close sits near the open, which is much clearer than placing it at the end of the function.

    The arguments to the deferred function are evaluated when the defer executes, not when the call executes. Besides avoiding worries about variables changing values as the function executes, this means that a single deferred call site can defer multiple function executions.

    Here’s a silly example.

    for i := 0; i < 5; i++ {
        defer fmt.Printf("%d ", i)
    }

    Deferred functions are executed in LIFO order, so this code will cause 4 3 2 1 0 to be printed when the function returns.

     
  • Kevin 3:18 pm on July 31, 2010 Permalink | Reply
    Tags:   

    How To Use Strings In Google Go 

    In the earlier tutorials, we have seen how to define variables and how to define constants in Google Go. This tutorial explains how we can use strings in Google Go programming.

    Strings
    The predeclared string type is string. Unlike C/C++ programming, Strings are length-delimited not NUL-terminated. Strings behave like arrays of bytes but are immutable: once created, it is impossible to change the contents of a string. In C++ terms, Go strings are a bit like const strings, while pointers to strings are analogous to const string references. Once you’ve built a string value, you can’t change it, although of course you can change a string variable simply by reassigning it.

    This snippet from strings.go is legal code:

    s := "hello"
    if s[1] != 'e' { os.Exit(1) }
    s = "good bye"
    var p *string = &s
    *p = "ciao"

    However the following statements are illegal because they would modify a string value:

    s[0] = 'x'
    (*p)[1] = 'y'

    The elements of strings have type byte and may be accessed using the usual indexing operations. It is illegal to take the address of such an element; if s[i] is theith byte of a string, &s[i] is invalid.

    String literals
    A string literal represents a string constant obtained from concatenating a sequence of characters. There are two forms: raw string literals and interpreted string literals.

    Raw string literals are character sequences between back quotes ` `. Within the quotes, any character is legal except back quote. When using back quotes, backslashes have no special meaning and the string may span multiple lines.

    Interpreted string literals are character sequences between double quotes ” “. The text between the quotes, which may not span multiple lines, forms the value of the literal, with backslash escapes interpreted as they are in character literals.

    Below are some examples of both types of string literals. Google Go also supports Unicode strings.

    `abc`  // same as "abc"
    `\n
    \n`    // same as "\\n\n\\n"
    "\n"
    ""
    "Hello, world!\n"
    "日本語"                                 // UTF-8 input text
    `日本語`                                 // UTF-8 input text as a raw literal

    Strings can be concatenated using the ‘+’ operator. The length of string s can be discovered using the built-in function len(). The length is a compile-time constant if s is a string literal.

    var string1 = "Hello World "
    var string2 = "This is Google Go"
    string3 := string1 + string2	//string3 = "Hello World This is Google Go"
    len_string3 := len(string3)	//len_string3 = 29

    Google Go provides a “strings” package that consists of several functions to manipulate strings. You can find more information at http://golang.org/pkg/strings/.

     
    • Jacob 4:44 am on August 1, 2010 Permalink | Reply

      I have been reading your tutorials Google Go. How would I prompt for user input and put it into a variable? I cannot seem to find how to do this. Thanks in advance.

      • Kevin 8:22 am on August 1, 2010 Permalink | Reply

        Hi Jacob,

        Thanks for reading Complete Coding.

        The “fmt” package available in Google Go implements formatted I/O with functions analogous to C’s printf and scanf.

        So if you want to prompt for user input and put it into a variable you can do something like,

        package main

        import “fmt”

        func main(){

        var a int

        fmt.Print(“Read a:”)
        fmt.Scan(&a)

        fmt.Println(” a =”, a)
        }

        There are many more functions available in the “fmt” package and you can find more information at http://golang.org/pkg/fmt/

        Keep reading Complete Coding for more tutorials on Google Go programming.

  • Kevin 10:35 pm on July 29, 2010 Permalink | Reply
    Tags:   

    How To Define Constants in Google Go 

    In the previous tutorial, we saw how to define variables in Google Go. In today’s tutorial, we check out how we can declare constants and enumerated constants in Google Go.

    Constants
    The types of constants available in Go programming are boolean constants, integer constants, floating-point constants, complex constants, and string constants. Constants in Go are created at compile time, even when defined as locals in functions.

    Constants are declared similar to variables except that the const keyword is used. Also we cannot use the idiom (using :=) as done for variable declaration.

    Below are examples of declaring different types of constants in Go programming.

    const Pi float64 = 3.14159265358979323846   //typed floating-point constant
    const zero = 0.0             // untyped floating-point constant
    const (
            size int64 = 1024   //typed integer constant
            eof = -1             // untyped integer constant
    )
    const a, b, c = 3, 4, "foo"  // a = 3, b = 4, c = "foo", untyped integer and string constants
    const u, v float = 0, 3      // u = 0.0, v = 3.0
    const sum = 1 – 0.707i   ///complex constant
    const flag bool = true

    Enumerated Constants
    In Go, enumerated constants are created using the iota enumerator. This can be considered similar to enumeration in C. It is reset to 0 whenever the reserved word const appears in the source and increments after each use of iota as shown below.

    const (  // iota is reset to 0
            c0 = iota  // c0 == 0
            c1 = iota  // c1 == 1
            c2 = iota  // c2 == 2
    )
    
    const (
            a = 1 << iota  // a == 1 (iota has been reset)
            b = 1 << iota  // b == 2
            c = 1 << iota  // c == 4
    )
    
    const (
            u       = iota * 42  // u == 0     (untyped integer constant)
            v float = iota * 42  // v == 42.0  (float constant)
            w       = iota * 42  // w == 84    (untyped integer constant)
    )
    
    const x = iota  // x == 0 (iota has been reset)
    const y = iota  // y == 0 (iota has been reset)
     
  • Kevin 9:38 pm on July 28, 2010 Permalink | Reply
    Tags:   

    How To Define Variables In Google Go 

    In the previous tutorial, we began by writing a Hello World program in Google Go. This tutorial will look into the available primitive data types and how to declare and define variables in Go programming.

    Basic Data Types
    The following are the basic data types that are available in the Google Go programming language.

    bool          boolean truth values of either true or false
    uint8         the set of all unsigned  8-bit integers (0 to 255)
    uint16       the set of all unsigned 16-bit integers (0 to 65535)
    uint32       the set of all unsigned 32-bit integers (0 to 4294967295)
    uint64       the set of all unsigned 64-bit integers (0 to 18446744073709551615)
    
    int8          the set of all signed  8-bit integers (-128 to 127)
    int16        the set of all signed 16-bit integers (-32768 to 32767)
    int32        the set of all signed 32-bit integers (-2147483648 to 2147483647)
    int64        the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
    
    float32     the set of all IEEE-754 32-bit floating-point numbers
    float64     the set of all IEEE-754 64-bit floating-point numbers
    
    complex64   the set of all complex numbers with float32 real and imaginary parts
    complex128  the set of all complex numbers with float64 real and imaginary parts
    
    byte        familiar alias for uint8
    
    uint         either 32 or 64 bits
    int          either 32 or 64 bits
    float       either 32 or 64 bits
    
    string      represents the set of string values

    To avoid portability issues all numeric types are distinct except byte, which is an alias for uint8. Conversions are required when different numeric types are mixed in an expression or assignment. For instance, int32 and int are not the same type even though they may have the same size on a particular architecture.

    Variables

    A computer variable can represent any kind of data that can be stored in a computer system.
    Variables in the Go programming language can be declared as,

        var s string = ""

    This is the var keyword, followed by the name of the variable, followed by its type, followed by an equals sign and an initial value for the variable.
    Go tries to be terse, and this declaration could be shortened. Since the string constant is of type string, we don’t have to tell the compiler that. We could write

        var s = ""

    or we could go even shorter and write the idiom

        s := ""

    Following are several example of declaring different types of variables in Go.

    var i int
    var U, V, W float
    var k = 0
    var x, y float = -1, -2
    var (
            i int
            u, v, s = 2.0, 3.0, "bar"
    )

    If no initial value is given to a variable, then that variable is initialized to it’s zero value. False for booleans, 0 for integers, 0.0 for floats, “” for strings, and nil for pointers, functions, interfaces, slices, channels, and maps.

    Type Conversions
    Go does not support implicit type conversion. To convert a numeric value from one type to another is a conversion, with syntax like a function call:

      uint8(int_var)     // truncate to size
      int(float_var)     // truncate fraction
      float64(int_var) // convert to float

    Also some conversions to string:

      string(0x1234)            // == "\u1234"
      string(array_of_bytes)    // bytes -> bytes
      string(array_of_ints)     // ints -> Unicode/UTF-8
     
  • Kevin 10:44 pm on July 26, 2010 Permalink | Reply
    Tags:   

    How To Write A Hello World Program In Google Go 

    In the previous post, we saw how to install Google’s Go programming language.

    Learning any programming language begins with the “Hello World” program. So keeping with the tradition and assuming that you have some programming background, here is the program written in the Google Go programming language.

    Open your favorite text editor and type the following program in it.

    package main
    
    import "fmt"   //Package implementing formatted I/O
    
    func main(){
       fmt.Printf("hello, world\n")
    }

    Save the file as “hello.go”.

    From the console, compile it using

    $ 6g hello.go

    To link the file, use

    $ 6l hello.6

    and to run it

    $ ./6.out

    This will print,

    hello, world

    The first line in the program

    package main

    specifies the name of the package that the file “hello.go” belongs to. The package keyword is used to define the package.

    This program imports the package “fmt” to gain access to fmt.Printf. We shall see more about packages in later tutorials.

    import "fmt"

    Functions are introduced with the func keyword. The main package’s main function is where the program starts running (after any initialization). We shall learn more about functions in later tutorials.

    func main()

    String constants can contain Unicode characters, encoded in UTF-8. The ‘\n’ is the newline character as in C/C++.

    The comment convention is the same as in C++:

    /* ... */
    // ...

    Did you find this tutorial on the Google Go programming language useful? Would you want more of such tutorials for learning about this great new programming language by Google?

     
  • Kevin 11:00 am on July 24, 2010 Permalink | Reply
    Tags:   

    How To Install Google's Go Programming Language 

    Introduction

    Go is a compiled, garbage-collected, concurrent programming language developed by Google.

    According to the Go website,

    • Go is simple.
    • Go is fast as typical builds take a fraction of a second and resulting programs run nearly as quickly as C or C++ code.
    • Go is type safe and memory safe.
    • Go promotes writing systems and servers as sets of lightweight communicating processes, called goroutines.
    • Go feels like a dynamic language but has the speed and safety of a static language.
    • Go is open source.

    Google feels that there is a need for another programming language because:

    • Computers are enormously quicker but software development is not faster.
    • Dependency management is a big part of software development today but the “header files” of languages in the C tradition are antithetical to clean dependency analysis—and fast compilation.
    • There is a growing rebellion against cumbersome type systems like those of Java and C++, pushing people towards dynamically typed languages such as Python and JavaScript.
    • Some fundamental concepts such as garbage collection and parallel computation are not well supported by popular systems languages.
    • The emergence of multicore computers has generated worry and confusion.

    Installation

    There are two Go compiler implementations, 6g and friends, generically called gc, and gccgo. The 6g (and 8g and 5g) compiler is named in the tradition of the Plan 9 C compilers, described in http://plan9.bell-labs.com/sys/doc/compiler.html6 is the architecture letter for amd64 (or x86-64, if you prefer), while g stands for Go. gccgo is a more traditional compiler using the gcc back end.

    Go implementations are currently available for the Linux and Mac OS X platforms. For Windows installation, you can try Go under MS Windows.

    This article focuses on installing the gc compiler for Ubuntu 10.04. However the steps should be similar for any Linux distribution.

    Installing the gc compiler:

    1) Set up the environment variables

    Set these variables in your shell profile ($HOME/.bashrc)

    export GOROOT=$HOME/go #The root of the Go tree
    export GOARCH=amd64 #The name of the target operating system (386, amd64, arm)
    export GOOS=linux #The name of the compilation architecture (darwin, freebsd, linux, nacl)
    export GOBIN=$HOME/go/bin #The location where binaries will be installed
    export PATH=$PATH:$GOBIN

    2) Install the C tools

    To build Go, you need to have GCC, the standard C libraries, the parser generator Bison, makeawk, and the text editor ed installed. On OS X, they can be installed as part of Xcode.

    $ sudo apt-get install bison gcc libc6-dev ed gawk make

    3) Fetch the repository

    You need Mercurial installed to get the Go repository. On Ubuntu, you can install this as:

    $ sudo apt-get install mercurial

    Make sure the $GOROOT directory does not exist or is empty. Then check out the repository:

    $ hg clone -r release https://go.googlecode.com/hg/ $GOROOT

    4) Install Go

    To build the Go distribution, run

    $ cd $GOROOT/src
    $ ./all.bash

    If all goes well, it will finish by printing

    --- cd ../test
    N known bugs; 0 unexpected bugs

    where N is a number that varies from release to release.

    5) Test the installation

    A sample program

    package main
    
    import "fmt"
    
    func main()
    {
       fmt.Printf("hello, world\n")
    }

    Compile it using

    $ 6g hello.go

    To link the file, use

    $ 6l hello.6

    and to run it

    $ ./6.out

    Now you should be able to compile and run programs written in the Go Programming language.

    6) Keeping up with releases

    New releases are announced on the Go Nuts mailing list. To update an existing tree to the latest release, you can run:

    $ cd $GOROOT/src
    $ hg pull
    $ hg update release
    $ ./all.bash

    You can get more details about installing the Go gc compiler here.

    Installing the gccgo compiler:

    You can get more details about installing the Go gccgo compiler here.

     
  • Kevin 8:10 pm on July 22, 2010 Permalink | Reply
    Tags:   

    The Ten Commandments for C++ Programmers 

    From the book “Practical C++ Programming” by Steve Oualline, The Ten Commandments for C++ Programmers written by Phin Straite.

    1. Thou shalt not rely on the compiler default methods for construction, destruction, copy construction, or assignment for any but the simplest of classes. Thou shalt forget these “big four” methods for any nontrivial class.

    2. Thou shalt declare and define thy destructor as virtual such that others may become heir to the fruits of your labors.

    3. Thou shalt not violate the “is-a” rule by abusing the inheritance mechanism for thine own twisted perversions.

    4. Thou shalt not rely on any implementation-dependent behavior of a compiler, operating system, or hardware environment, lest thy code be forever caged within that dungeon.

    5. Thou shalt not augment the interface of a class at the lowest level without most prudent deliberation. Such ill-begotten practices imprison thy clients unjustly into thy classes and create unrest when code maintenance and extension are required.

    6. Thou shalt restrict thy friendship to truly worthy contemporaries. Beware, for thou art exposing thyself rudely as from a trenchcoat.

    7. Thou shalt not abuse thy implementation data by making it public or static except in the rarest of circumstances. Thy data are thine own; share it not with others.

    8. Thou shalt not suffer dangling pointers or references to be harbored within thy objects. They are nefarious and precarious agents of random and wanton destruction.

    9. Thou shalt make use of available class libraries as conscientiously as possible. Code reuse, not just thine own but that of thy clients as well, is the Holy Grail of OO.

    10. Thou shalt forever forswear the use of the vile printf/scanf, rather favoring the flowing streams. Cast off thy vile C cloak and partake of the wondrous fruit of flexible and extensible I/O.

     
    • stevej 7:46 pm on July 27, 2010 Permalink | Reply

      #3,#7,and#9 are not C++-specific. Just plain ol’ oop.

      #7: ‘struct’ is ‘class’ for public data members

      #8 applies to object references in general (therefore java, etc.). Dangling pointer are trouble in non-oo languages as well (C, Pascal, etc).

      #1: occasionally the default impls are fine. Just know what you’re saying (see: Meyers, Saks).

      It’s a tricky and maligned language for sure. Not sure that the convoluted English or the “10 commandments” format will win over any new “converts” however.

      • Kevin 7:57 pm on July 28, 2010 Permalink | Reply

        Thanks for your comment, stevej. You have mentioned valid points there.

        This list of the ten commandments for C++ programmers has been taken from the “Practical C++ Programming” book.

        The purpose of these points is not to have C++ converts. Rather it provides some basic points that C++ programmers could trip over and so should keep in mind while C++ programming.

  • Kevin 9:27 pm on July 20, 2010 Permalink | Reply
    Tags:   

    Today's Read: Four Ways To A Practical Code Review 

    As per the definition of Wikipedia,

    Code review is systematic examination (often as peer review) of computer source code intended to find and fix mistakes overlooked in the initial development phase, improving both the overall quality of software and the developers’ skills.

    In this article, Jason Cohen mentions that there exists a formalized system of code review developed by Micheal Fagan at IBM in the mid-1970s. However he points out that the software development process has come a long way since then and we can have process and metrics and measurement and improvement and happy developers all at the same time. The way to achieve this is by using a lightweight code review.

    There are four types of lightweight code review techniques:

    1) Over-the-shoulder: One developer looks over the author’s shoulder as the latter walks through the code.
    This is the code review technique that we use in our organization and that I am familiar with. Once the implementation has reached a level of completion, we initiate such an over-the-shoulder code review process. One of the experienced peers working on the project acts as the reviewer and sits alongside the developer. The developer walksthrough the code implementation explaining why and how the implementation has been done. The reviewer can interrupt the review process to ask any queries and point out discrepancies in the implementation. Small changes can be fixed by the developer during the review process. Larger changes can be taken offline. Once the code review is complete, the developer can check in the changes to the SCM system.

    2) Email pass-around: The author (or SCM system) emails code to reviewers.
    This is the second-most common form of lightweight code review, and the technique preferred by most open-source projects. Here, whole files or changes are packaged up by the author and sent to reviewers via email. Reviewers examine the files, ask questions and discuss with the author and other developers, and suggest changes.

    3) Pair Programming: Two authors develop code together at the same workstation.
    Pair-programming is two developers writing code at a single workstation with only one developer typing at a time and continuous free-form discussion and review.

    4) Tool-assisted: Authors and reviewers use specialized tools designed for peer code review.
    This refers to any process where specialized tools are used in all aspects of the review: collecting files, transmitting and displaying files, commentary, and defects among all participants, collecting metrics, and giving product managers and administrators some control over the workflow.

    The complete article by Jason Cohen can be found at: Four ways to a Practical Code Review.

     
  • Kevin 10:14 am on July 18, 2010 Permalink | Reply
    Tags:   

    The History Of Carriage Return Line Feed 

    Most of us developers (especially Windows developers) wonder why an end-of-line is represented by <carriage return><line feed> in Windows text files. What does <carriage return> or <line feed> exactly mean?

    In the early stages before computers, there existed a device known as the TeleType. The TeleType contained a keyboard, printer, and paper tape reader/punch. It could transmit messages over telephones using a modem at a rate of 10 characters per second.

    TeleType

    But TeleType had a problem. It took 0.2 seconds to move the printhead from the right side to the left. 0.2 seconds is two character times. If a second character came while the printhead was in the middle of a return, that character was lost.

    The TeleType people solved this problem by making end-of-line two characters: <carriage return> to position the printhead at the left margin, and <line feed> to move the paper up one line. That way the <line feed> “printed” whilethe printhead was racing back to the left margin.

    When the early computers came out, some designers realized that using two characters for end-of-line wasted storage. Some picked <line feed> for their end-of-line, and some chose <carriage return>. Some of the die-hards stayed with the two-character sequence.

    Unix uses <line feed> for end-of-line. The newline character \n is code 0xA (LF or <line feed>). MS-DOS/Windows uses the two characters <carriage return><line feed>. So if you are transferring text files from a Unix machine to Windows, you have to use some conversion utility eg. unix2dos for stripping the <carriage return> character from the files.

    Further reading:
    How do I convert between Unix and Windows text files?
    Newline Character

     
c
compose new post
j
next post/next comment
k
previous post/previous comment
r
reply
e
edit
o
show/hide comments
t
go to top
l
go to login
h
show/hide help
shift + esc
cancel
Follow

Get every new post delivered to your Inbox.