Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const romanNumeralDict map[int]string = {
- 1000: "M",
- 900 : "CM",
- 500 : "D",
- 400 : "CD",
- 100 : "C",
- 90 : "XC",
- 50 : "L",
- 40 : "XL",
- 10 : "X",
- 9 : "IX",
- 5 : "V",
- 4 : "IV",
- 1 : "I",
- }
- # command-line-arguments
- ./Roman_Numerals.go:9: syntax error: unexpected {
- var romanNumeralDict = map[int]string{
- 1000: "M",
- 900 : "CM",
- 500 : "D",
- 400 : "CD",
- 100 : "C",
- 90 : "XC",
- 50 : "L",
- 40 : "XL",
- 10 : "X",
- 9 : "IX",
- 5 : "V",
- 4 : "IV",
- 1 : "I",
- }
- romanNumeralDict := map[int]string{
- ...
- const myString = "hello"
- const pi = 3.14 // untyped constant
- const life int = 42 // typed constant (can use only with ints)
- const (
- First = 1
- Second = 2
- Third = 4
- )
- package main
- import (
- "fmt"
- )
- // http://stackoverflow.com/a/27457144/10278
- func romanNumeralDict() func(int) string {
- // innerMap is captured in the closure returned below
- innerMap := map[int]string{
- 1000: "M",
- 900: "CM",
- 500: "D",
- 400: "CD",
- 100: "C",
- 90: "XC",
- 50: "L",
- 40: "XL",
- 10: "X",
- 9: "IX",
- 5: "V",
- 4: "IV",
- 1: "I",
- }
- return func(key int) string {
- return innerMap[key]
- }
- }
- func main() {
- fmt.Println(romanNumeralDict()(10))
- fmt.Println(romanNumeralDict()(100))
- dict := romanNumeralDict()
- fmt.Println(dict(400))
- }
- var romanNumeralDict = struct {
- m map[int]string
- }{m: map[int]string {
- 1000: "M",
- 900: "CM",
- //YOUR VALUES HERE
- }}
- func main() {
- d := 1000
- fmt.Printf("Value of Key (%d): %s", d, romanNumeralDict.m[1000])
- }
- // romanNumeralDict returns map[int]string dictionary, since the return
- // value is always the same it gives the pseudo-constant output, which
- // can be referred to in the same map-alike fashion.
- var romanNumeralDict = func() map[int]string { return map[int]string {
- 1000: "M",
- 900: "CM",
- 500: "D",
- 400: "CD",
- 100: "C",
- 90: "XC",
- 50: "L",
- 40: "XL",
- 10: "X",
- 9: "IX",
- 5: "V",
- 4: "IV",
- 1: "I",
- }
- }
- func printRoman(key int) {
- fmt.Println(romanNumeralDict()[key])
- }
- func printKeyN(key, n int) {
- fmt.Println(strings.Repeat(romanNumeralDict()[key], n))
- }
- func main() {
- printRoman(1000)
- printRoman(10)
- }
Add Comment
Please, Sign In to add comment