RCHQ9+/Go

From Rosetta Code
Revision as of 19:11, 18 April 2018 by Sonia (talk | contribs) (Update example session for current go tooling. (Remarkably, the source code does not need an update. It was posted in 2011, pre-Go 1.0, and still compiles and runs just fine today with the most recent version of Go.))
RCHQ9+/Go is an implementation of HQ9+. Other implementations of HQ9+.
RCHQ9+/Go is part of RCHQ9+. You may find other members of RCHQ9+ at Category:RCHQ9+.

This interpreter accepts HQ9+ source as the first command line argument. <lang go>package main

import (

   "fmt"
   "os"

)

func main() {

   if len(os.Args) < 2 {
       return
   }
   for _, c := range os.Args[1] {
       switch c {
       case 'H':
           fmt.Println("hello, world")
       case 'Q':
           fmt.Printf("%s%c%s%c\n", q, 96, q, 96)
       case '9':
           const l1t = " of beer on the wall"
           const l2t = " of beer.\nTake one down, pass it around,\n"
           const l1p = " bottles"+l1t
           const l2p = " bottles"+l2t
           fmt.Print(99, l1p, ",\n", 99, l2p)
           for n := 98; n > 1; n-- {
               fmt.Print(n, l1p, ".\n\n", n, l1p, ",\n", n, l2p)
           }
           fmt.Print("One bottle"+l1t+".\n\n")
           fmt.Print("One bottle"+l1t+",\nOne bottle"+l2t+"No"+l1p+".\n")
       case '+':
           a++
       }
   }

}

var a int var q = `package main

import (

   "fmt"
   "os"

)

func main() {

   if len(os.Args) < 2 {
       return
   }
   for _, c := range os.Args[1] {
       switch c {
       case 'H':
           fmt.Println("hello, world")
       case 'Q':
           fmt.Printf("%s%c%s%c\n", q, 96, q, 96)
       case '9':
           const l1t = " of beer on the wall"
           const l2t = " of beer.\nTake one down, pass it around,\n"
           const l1p = " bottles"+l1t
           const l2p = " bottles"+l2t
           fmt.Print(99, l1p, ",\n", 99, l2p)
           for n := 98; n > 1; n-- {
               fmt.Print(n, l1p, ".\n\n", n, l1p, ",\n", n, l2p)
           }
           fmt.Print("One bottle"+l1t+".\n\n")
           fmt.Print("One bottle"+l1t+",\nOne bottle"+l2t+"No"+l1p+".\n")
       case '+':
           a++
       }
   }

}

var a int var q = `</lang>

Example session building and running program:
$ go build HQ9+.go
$ ./HQ9+ H+H
hello, world
hello, world
Another, running Q output:
$ ./HQ9+ Q >q.go
$ go run q.go H
hello, world