WeixiangYan/CodeTransOcean huggingface dataset:
Task: Native shebang

C: #!/usr/local/bin/script_gcc.sh

#include <errno.h>
#include <libgen.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>




typedef char  *STRING;
typedef enum{FALSE=0, TRUE=1} BOOL;
const STRING ENDCAT = NULL;


#define DIALECT "c" 
const STRING
  CC="gcc",
  COPTS="-lm -x "DIALECT,
  IEXT="."DIALECT,
  OEXT=".out";
const BOOL OPT_CACHE = TRUE;


char strcat_out[BUFSIZ];

STRING STRCAT(STRING argv, ... ){
  va_list ap;
  va_start(ap, argv);
  STRING arg;
  strcat_out[0]='\0';
  for(arg=argv; arg != ENDCAT; arg=va_arg(ap, STRING)){
     strncat(strcat_out, arg, sizeof strcat_out);
  }
  va_end(ap);
  return strndup(strcat_out, sizeof strcat_out);
}

char itoa_out[BUFSIZ];

STRING itoa_(int i){
  sprintf(itoa_out, "%d", i);
  return itoa_out;
}

time_t modtime(STRING filename){
  struct stat buf;
  if(stat(filename, &buf) != EXIT_SUCCESS)perror(filename);
  return buf.st_mtime;
}


BOOL compile(STRING srcpath, STRING binpath){
  int out;
  STRING compiler_command=STRCAT(CC, " ", COPTS, " -o ", binpath, " -", ENDCAT);
  FILE *src=fopen(srcpath, "r"),
       *compiler=popen(compiler_command, "w");
  char buf[BUFSIZ];
  BOOL shebang;

  for(shebang=TRUE; fgets(buf, sizeof buf, src); shebang=FALSE)
    if(!shebang)fwrite(buf, strlen(buf), 1, compiler);

  out=pclose(compiler);
  return out;
}

void main(int argc, STRING *argv, STRING *envp){

  STRING binpath,
         srcpath=argv[1],
         argv0_basename=STRCAT(basename((char*)srcpath ), ENDCAT),
         *dirnamew, *dirnamex;
  argv++; 


  STRING paths[] = {
    dirname(strdup(srcpath)), 
    STRCAT(getenv("HOME"), "/bin", ENDCAT),
    "/usr/local/bin",
    ".",
    STRCAT(getenv("HOME"), "/tmp", ENDCAT),
    getenv("HOME"),
    STRCAT(getenv("HOME"), "/Desktop", ENDCAT),

    ENDCAT
  };

  for(dirnamew = paths; *dirnamew; dirnamew++){
    if(access(*dirnamew, W_OK) == EXIT_SUCCESS) break;
  }


  if(OPT_CACHE == FALSE){
    binpath=STRCAT(*dirnamew, "/", argv0_basename, itoa_(getpid()), OEXT, ENDCAT);
    if(compile(srcpath, binpath) == EXIT_SUCCESS){
      if(fork()){
        sleep(0.1); unlink(binpath);
      } else {
        execvp(binpath, argv);
      }
    }
  } else {

    time_t modtime_srcpath = modtime(srcpath);
    for(dirnamex = paths; *dirnamex; dirnamex++){
      binpath=STRCAT(*dirnamex, "/", argv0_basename, OEXT, ENDCAT);
      if((access(binpath, X_OK) == EXIT_SUCCESS) && (modtime(binpath) >= modtime_srcpath))
        execvp(binpath, argv);
    }
  }

  binpath=STRCAT(*dirnamew, "/", argv0_basename, OEXT, ENDCAT);
  if(compile(srcpath, binpath) == EXIT_SUCCESS)
    execvp(binpath, argv);

  perror(STRCAT(binpath, ": executable not available", ENDCAT));
  exit(errno);
}




Go: 
package main

import (
    "fmt"
    "os"
)

func main() {
    if len(os.Args) > 1 {
        fmt.Println(os.Args[1])
    }
}
Task: EKG sequence convergence

C: #include <stdio.h>
#include <stdlib.h>

#define TRUE 1
#define FALSE 0
#define LIMIT 100

typedef int bool;

int compareInts(const void *a, const void *b) {
    int aa = *(int *)a;
    int bb = *(int *)b;
    return aa - bb;
}

bool contains(int a[], int b, size_t len) {
    int i;
    for (i = 0; i < len; ++i) {
        if (a[i] == b) return TRUE;
    }
    return FALSE;
}

int gcd(int a, int b) {
    while (a != b) {
        if (a > b)
            a -= b;
        else
            b -= a;
    }
    return a;
}

bool areSame(int s[], int t[], size_t len) {
    int i;
    qsort(s, len, sizeof(int), compareInts);    
    qsort(t, len, sizeof(int), compareInts);
    for (i = 0; i < len; ++i) {
        if (s[i] != t[i]) return FALSE;
    }
    return TRUE;
}

int main() {
    int s, n, i;
    int starts[5] = {2, 5, 7, 9, 10};
    int ekg[5][LIMIT];
    for (s = 0; s < 5; ++s) {
        ekg[s][0] = 1;
        ekg[s][1] = starts[s];
        for (n = 2; n < LIMIT; ++n) {
            for (i = 2; ; ++i) {
                
                
                if (!contains(ekg[s], i, n) && gcd(ekg[s][n - 1], i) > 1) {
                    ekg[s][n] = i;
                    break;
                }
            }
        }
        printf("EKG(%2d): [", starts[s]);
        for (i = 0; i < 30; ++i) printf("%d ", ekg[s][i]);
        printf("\b]\n");
    }
    
    
    for (i = 2; i < LIMIT; ++i) {
        if (ekg[1][i] == ekg[2][i] && areSame(ekg[1], ekg[2], i)) {
            printf("\nEKG(5) and EKG(7) converge at term %d\n", i + 1);
            return 0;
        }
    }
    printf("\nEKG5(5) and EKG(7) do not converge within %d terms\n", LIMIT);
    return 0;
}




Go: package main

import (
    "fmt"
    "sort"
)

func contains(a []int, b int) bool {
    for _, j := range a {
        if j == b {
            return true
        }
    }
    return false
}

func gcd(a, b int) int {
    for a != b {
        if a > b {
            a -= b
        } else {
            b -= a
        }
    }
    return a
}

func areSame(s, t []int) bool {
    le := len(s)
    if le != len(t) {
        return false
    }
    sort.Ints(s)
    sort.Ints(t)
    for i := 0; i < le; i++ {
        if s[i] != t[i] {
            return false
        }
    }
    return true
}

func main() {
    const limit = 100
    starts := [5]int{2, 5, 7, 9, 10}
    var ekg [5][limit]int

    for s, start := range starts {
        ekg[s][0] = 1
        ekg[s][1] = start
        for n := 2; n < limit; n++ {
            for i := 2; ; i++ {
                
                
                if !contains(ekg[s][:n], i) && gcd(ekg[s][n-1], i) > 1 {
                    ekg[s][n] = i
                    break
                }
            }
        }
        fmt.Printf("EKG(%2d): %v\n", start, ekg[s][:30])
    }   

    
    for i := 2; i < limit; i++ {
        if ekg[1][i] == ekg[2][i] && areSame(ekg[1][:i], ekg[2][:i]) {
            fmt.Println("\nEKG(5) and EKG(7) converge at term", i+1)
            return
        }
    }
    fmt.Println("\nEKG5(5) and EKG(7) do not converge within", limit, "terms")
}
Task: Rep-string

C: #include <stdio.h>
#include <string.h>

int repstr(char *str)
{
    if (!str) return 0;

    size_t sl = strlen(str) / 2;
    while (sl > 0) {
        if (strstr(str, str + sl) == str)
            return sl;
        --sl;
    }

    return 0;
}

int main(void)
{
    char *strs[] = { "1001110011", "1110111011", "0010010010", "1111111111",
        "0100101101", "0100100", "101", "11", "00", "1" };

    size_t strslen = sizeof(strs) / sizeof(strs[0]);
    size_t i;
    for (i = 0; i < strslen; ++i) {
        int n = repstr(strs[i]);
        if (n)
            printf("\"%s\" = rep-string \"%.*s\"\n", strs[i], n, strs[i]);
        else
            printf("\"%s\" = not a rep-string\n", strs[i]);
    }

    return 0;
}




Go: package main

import (
    "fmt"
    "strings"
)

func rep(s string) int {
    for x := len(s) / 2; x > 0; x-- {
        if strings.HasPrefix(s, s[x:]) {
            return x
        }
    }
    return 0
}

const m = `
1001110011
1110111011
0010010010
1010101010
1111111111
0100101101
0100100
101
11
00
1`

func main() {
    for _, s := range strings.Fields(m) {
        if n := rep(s); n > 0 {
            fmt.Printf("%q  %d rep-string %q\n", s, n, s[:n])
        } else {
            fmt.Printf("%q  not a rep-string\n", s)
        }
    }
}
Task: Terminal control_Preserve screen

C: #include <stdio.h>
#include <unistd.h>

int main()
{
	int i;
	printf("\033[?1049h\033[H");
	printf("Alternate screen buffer\n");
	for (i = 5; i; i--) {
		printf("\rgoing back in %d...", i);
		fflush(stdout);
		sleep(1);
	}
	printf("\033[?1049l");

	return 0;
}




Go: package main

import (
    "fmt"
    "time"
)

func main() {
    fmt.Print("\033[?1049h\033[H")
    fmt.Println("Alternate screen buffer\n")
    s := "s"
    for i := 5; i > 0; i-- {
        if i == 1 {
            s = ""
        }
        fmt.Printf("\rgoing back in %d second%s...", i, s)
        time.Sleep(time.Second)
    }
    fmt.Print("\033[?1049l")
}
Task: Literals_String

C: char ch = 'z';




Go: ch := 'z'
ch = 122          
ch = '\x7a'       
ch = '\u007a'     
ch = '\U0000007a' 
ch = '\172'
Task: Monads_List monad

C: #include <stdio.h>
#include <stdlib.h>

#define MONAD void*
#define INTBIND(f, g, x) (f((int*)g(x)))
#define RETURN(type,x) &((type)*)(x)

MONAD boundInt(int *x) {
    return (MONAD)(x);
}

MONAD boundInt2str(int *x) {
    char buf[100];
    char*str= malloc(1+sprintf(buf, "%d", *x));
    sprintf(str, "%d", *x);
    return (MONAD)(str);
}

void task(int y) {
    char *z= INTBIND(boundInt2str, boundInt, &y);
    printf("%s\n", z);
    free(z);
}

int main() {
    task(13);
}




Go: package main

import "fmt"

type mlist struct{ value []int }

func (m mlist) bind(f func(lst []int) mlist) mlist {
    return f(m.value)
}

func unit(lst []int) mlist {
    return mlist{lst}
}

func increment(lst []int) mlist {
    lst2 := make([]int, len(lst))
    for i, v := range lst {
        lst2[i] = v + 1
    }
    return unit(lst2)
}

func double(lst []int) mlist {
    lst2 := make([]int, len(lst))
    for i, v := range lst {
        lst2[i] = 2 * v
    }
    return unit(lst2)
}

func main() {
    ml1 := unit([]int{3, 4, 5})
    ml2 := ml1.bind(increment).bind(double)
    fmt.Printf("%v -> %v\n", ml1.value, ml2.value)
}
Task: Find squares n where n+1 is prime

C: #include <stdio.h>
#include <stdbool.h>
#include <math.h>

#define MAX 1000

void sieve(int n, bool *prime) {
    prime[0] = prime[1] = false;
    for (int i=2; i<=n; i++) prime[i] = true;
    for (int p=2; p*p<=n; p++) 
        if (prime[p])
            for (int c=p*p; c<=n; c+=p) prime[c] = false;
}

bool square(int n) {
    int sq = sqrt(n);
    return (sq * sq == n);
}

int main() {
    bool prime[MAX + 1];
    sieve(MAX, prime);
    for (int i=2; i<=MAX; i++) if (prime[i]) {
        int sq = i-1;
        if (square(sq)) printf("%d ", sq);
    }
    printf("\n");
    return 0;
}




Go: package main

import (
    "fmt"
    "math"
    "rcu"
)

func main() {
    var squares []int
    limit := int(math.Sqrt(1000))
    i := 1
    for i <= limit {
        n := i * i
        if rcu.IsPrime(n + 1) {
            squares = append(squares, n)
        }
        if i == 1 {
            i = 2
        } else {
            i += 2
        }
    }
    fmt.Println("There are", len(squares), "square numbers 'n' where 'n+1' is prime, viz:")
    fmt.Println(squares)
}
Task: Next special primes

C: #include <stdio.h>
#include <stdbool.h>

bool isPrime(int n) {
    int d;
    if (n < 2)  return false;
    if (!(n%2)) return n == 2;
    if (!(n%3)) return n == 3;
    d = 5;
    while (d*d <= n) {
        if (!(n%d)) return false;
        d += 2;
        if (!(n%d)) return false;
        d += 4;
    }
    return true;
}

int main() {
    int i, lastSpecial = 3, lastGap = 1;
    printf("Special primes under 1,050:\n");
    printf("Prime1 Prime2 Gap\n");
    printf("%6d %6d %3d\n", 2, 3, lastGap);
    for (i = 5; i < 1050; i += 2) {
        if (isPrime(i) && (i-lastSpecial) > lastGap) {
            lastGap = i - lastSpecial;
            printf("%6d %6d %3d\n", lastSpecial, i, lastGap);
            lastSpecial = i;
        }
    }
}




Go: package main

import "fmt"

func sieve(limit int) []bool {
    limit++
    
    c := make([]bool, limit) 
    c[0] = true
    c[1] = true
    
    p := 3 
    for {
        p2 := p * p
        if p2 >= limit {
            break
        }
        for i := p2; i < limit; i += 2 * p {
            c[i] = true
        }
        for {
            p += 2
            if !c[p] {
                break
            }
        }
    }
    return c
}

func main() {
    c := sieve(1049)
    fmt.Println("Special primes under 1,050:")
    fmt.Println("Prime1 Prime2 Gap")
    lastSpecial := 3
    lastGap := 1
    fmt.Printf("%6d %6d %3d\n", 2, 3, lastGap)
    for i := 5; i < 1050; i += 2 {
        if !c[i] && (i-lastSpecial) > lastGap {
            lastGap = i - lastSpecial
            fmt.Printf("%6d %6d %3d\n", lastSpecial, i, lastGap)
            lastSpecial = i
        }
    }
}
Task: Special neighbor primes

C: #include<stdio.h>
#include<stdlib.h>

int isprime( int p ) {
    int i;
    if(p==2) return 1;
    if(!(p%2)) return 0;
    for(i=3; i*i<=p; i+=2) {
       if(!(p%i)) return 0;
    }
    return 1;
}

int nextprime( int p ) {
    int i=0;
    if(p==0) return 2;
    if(p<3) return p+1;
    while(!isprime(++i + p));
    return i+p;
}

int main(void) {
    int p1, p2;
    for(p1=3;p1<=99;p1+=2) {
        p2=nextprime(p1);
        if(p2<100&&isprime(p1)&&isprime(p2+p1-1)) {
            printf( "%d + %d - 1 = %d\n", p1, p2, p1+p2-1 );
        }
    }
    return 0;
}




Go: package main

import (
    "fmt"
    "rcu"
)

const MAX = 1e7 - 1

var primes = rcu.Primes(MAX)

func specialNP(limit int, showAll bool) {
    if showAll {
        fmt.Println("Neighbor primes, p1 and p2, where p1 + p2 - 1 is prime:")
    }
    count := 0
    for i := 1; i < len(primes); i++ {
        p2 := primes[i]
        if p2 >= limit {
            break
        }
        p1 := primes[i-1]
        p3 := p1 + p2 - 1
        if rcu.IsPrime(p3) {
            if showAll {
                fmt.Printf("(%2d, %2d) => %3d\n", p1, p2, p3)
            }
            count++
        }
    }
    ccount := rcu.Commatize(count)
    climit := rcu.Commatize(limit)
    fmt.Printf("\nFound %s special neighbor primes under %s.\n", ccount, climit)
}

func main() {
    specialNP(100, true)
    var pow = 1000
    for i := 3; i < 8; i++ {
        specialNP(pow, false)
        pow *= 10
    }
}
Task: Ramsey's theorem

C: #include <stdio.h>

int a[17][17], idx[4];

int find_group(int type, int min_n, int max_n, int depth)
{
	int i, n;
	if (depth == 4) {
		printf("totally %sconnected group:", type ? "" : "un");
		for (i = 0; i < 4; i++) printf(" %d", idx[i]);
		putchar('\n');
		return 1;
	}

	for (i = min_n; i < max_n; i++) {
		for (n = 0; n < depth; n++)
			if (a[idx[n]][i] != type) break;

		if (n == depth) {
			idx[n] = i;
			if (find_group(type, 1, max_n, depth + 1))
				return 1;
		}
	}
	return 0;
}

int main()
{
	int i, j, k;
	const char *mark = "01-";

	for (i = 0; i < 17; i++)
		a[i][i] = 2;

	for (k = 1; k <= 8; k <<= 1) {
		for (i = 0; i < 17; i++) {
			j = (i + k) % 17;
			a[i][j] = a[j][i] = 1;
		}
	}

	for (i = 0; i < 17; i++) {
		for (j = 0; j < 17; j++)
			printf("%c ", mark[a[i][j]]);
		putchar('\n');
	}

	
	

	
	for (i = 0; i < 17; i++) {
		idx[0] = i;
		if (find_group(1, i+1, 17, 1) || find_group(0, i+1, 17, 1)) {
			puts("no good");
			return 0;
		}
	}
	puts("all good");
	return 0;
}




Go: package main

import "fmt"

var (
    a   [17][17]int
    idx [4]int
)

func findGroup(ctype, min, max, depth int) bool {
    if depth == 4 {
        cs := ""
        if ctype == 0 {
            cs = "un"
        }
        fmt.Printf("Totally %sconnected group:", cs)
        for i := 0; i < 4; i++ {
            fmt.Printf(" %d", idx[i])
        }
        fmt.Println()
        return true
    }

    for i := min; i < max; i++ {
        n := 0
        for ; n < depth; n++ {
            if a[idx[n]][i] != ctype {
                break
            }
        }

        if n == depth {
            idx[n] = i
            if findGroup(ctype, 1, max, depth+1) {
                return true
            }
        }
    }
    return false
}

func main() {
    const mark = "01-"

    for i := 0; i < 17; i++ {
        a[i][i] = 2
    }

    for k := 1; k <= 8; k <<= 1 {
        for i := 0; i < 17; i++ {
            j := (i + k) % 17
            a[i][j], a[j][i] = 1, 1
        }
    }

    for i := 0; i < 17; i++ {
        for j := 0; j < 17; j++ {
            fmt.Printf("%c ", mark[a[i][j]])
        }
        fmt.Println()
    }

    
    

    
    for i := 0; i < 17; i++ {
        idx[0] = i
        if findGroup(1, i+1, 17, 1) || findGroup(0, i+1, 17, 1) {
            fmt.Println("No good.")
            return
        }
    }
    fmt.Println("All good.")
}
Task: GUI_Maximum window dimensions

C: #include<windows.h>
#include<stdio.h>

int main()
{
	printf("Dimensions of the screen are (w x h) : %d x %d pixels",GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN));
	return 0;
}




Go: package main

import (
    "fmt"
    "github.com/go-vgo/robotgo"
)

func main() {
    w, h := robotgo.GetScreenSize()
    fmt.Printf("Screen size: %d x %d\n", w, h)
    fpid, err := robotgo.FindIds("firefox")
    if err == nil && len(fpid) > 0 {
        pid := fpid[0]
        robotgo.ActivePID(pid)
        robotgo.MaxWindow(pid)
        _, _, w, h = robotgo.GetBounds(pid)
        fmt.Printf("Max usable : %d x %d\n", w, h)
    }
}
Task: Terminal control_Inverse video

C: #include <stdio.h>

int main()
{
	printf("\033[7mReversed\033[m Normal\n");

	return 0;
}




Go: package main

import (
    "fmt"
    "os"
    "os/exec"
)

func main() {
    tput("rev")
    fmt.Print("Rosetta")
    tput("sgr0")
    fmt.Println(" Code")
}

func tput(arg string) error {
    cmd := exec.Command("tput", arg)
    cmd.Stdout = os.Stdout
    return cmd.Run()
}
Task: Using a speech engine to highlight words

C: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "wren.h"

void C_usleep(WrenVM* vm) {
    useconds_t usec = (useconds_t)wrenGetSlotDouble(vm, 1);
    usleep(usec);
}

void C_espeak(WrenVM* vm) {
    const char *arg = wrenGetSlotString(vm, 1);
    char command[strlen(arg) + 10];
    strcpy(command, "espeak \"");
    strcat(command, arg);
    strcat(command, "\"");
    system(command);
}

void C_flushStdout(WrenVM* vm) {
    fflush(stdout);
}

WrenForeignMethodFn bindForeignMethod(
    WrenVM* vm,
    const char* module,
    const char* className,
    bool isStatic,
    const char* signature) {
    if (strcmp(module, "main") == 0) {
        if (strcmp(className, "C") == 0) {
            if (isStatic && strcmp(signature, "usleep(_)") == 0)     return C_usleep;
            if (isStatic && strcmp(signature, "espeak(_)") == 0)     return C_espeak;
            if (isStatic && strcmp(signature, "flushStdout()") == 0) return C_flushStdout;
        }
    }
    return NULL;
}

static void writeFn(WrenVM* vm, const char* text) {
    printf("%s", text);
}

char *readFile(const char *fileName) {
    FILE *f = fopen(fileName, "r");
    fseek(f, 0, SEEK_END);
    long fsize = ftell(f);
    rewind(f);
    char *script = malloc(fsize + 1);
    fread(script, 1, fsize, f);
    fclose(f);
    script[fsize] = 0;
    return script;
}

static void loadModuleComplete(WrenVM* vm, const char* module, WrenLoadModuleResult result) {
    if( result.source) free((void*)result.source);
}

WrenLoadModuleResult loadModule(WrenVM* vm, const char* name) {
    WrenLoadModuleResult result = {0};
    if (strcmp(name, "random") != 0 && strcmp(name, "meta") != 0) {
        result.onComplete = loadModuleComplete;
        char fullName[strlen(name) + 6];
        strcpy(fullName, name);
        strcat(fullName, ".wren");
        result.source = readFile(fullName);
    }
    return result;
}

int main(int argc, char **argv) {
    WrenConfiguration config;
    wrenInitConfiguration(&config);
    config.writeFn = &writeFn;
    config.bindForeignMethodFn = &bindForeignMethod;
    config.loadModuleFn = &loadModule;
    WrenVM* vm = wrenNewVM(&config);
    const char* module = "main";
    const char* fileName = "speech_engine_highlight_words.wren";
    char *script = readFile(fileName);
    wrenInterpret(vm, module, script);
    wrenFreeVM(vm);
    free(script);
    return 0;
}




Go: package main

import (
    "fmt"
    "log"
    "os/exec"
    "strings"
    "time"
)

func main() {
    s := "Actions speak louder than words."
    prev := ""
    prevLen := 0
    bs := ""
    for _, word := range strings.Fields(s) {
        cmd := exec.Command("espeak", word)
        if err := cmd.Run(); err != nil {
            log.Fatal(err)
        }
        if prevLen > 0 {
            bs = strings.Repeat("\b", prevLen)
        }
        fmt.Printf("%s%s%s ", bs, prev, strings.ToUpper(word))
        prev = word + " "
        prevLen = len(word) + 1
    }
    bs = strings.Repeat("\b", prevLen)
    time.Sleep(time.Second)
    fmt.Printf("%s%s\n", bs, prev)
}
Task: Getting the number of decimals

C: #include <stdio.h>

int findNumOfDec(double x) {
    char buffer[128];
    int pos, num;

    sprintf(buffer, "%.14f", x);

    pos = 0;
    num = 0;
    while (buffer[pos] != 0 && buffer[pos] != '.') {
        pos++;
    }
    if (buffer[pos] != 0) {
        pos++; 
        while (buffer[pos] != 0) {
            pos++; 
        }
        pos--; 
        while (buffer[pos] == '0') {
            pos--; 
        }
        while (buffer[pos] != '.') {
            num++;
            pos--; 
        }
    }
    return num;
}

void test(double x) {
    int num = findNumOfDec(x);
    printf("%f has %d decimals\n", x, num);
}

int main() {
    test(12.0);
    test(12.345);
    test(12.345555555555);
    test(12.3450);
    test(12.34555555555555555555);
    test(1.2345e+54);
    return 0;
}




Go: package main

import (
    "fmt"
    "log"
    "math"
    "strings"
)

var error = "Argument must be a numeric literal or a decimal numeric string."

func getNumDecimals(n interface{}) int {
    switch v := n.(type) {
    case int:
        return 0
    case float64:
        if v == math.Trunc(v) {
            return 0
        }
        s := fmt.Sprintf("%g", v)
        return len(strings.Split(s, ".")[1])
    case string:
        if v == "" {
            log.Fatal(error)
        }
        if v[0] == '+' || v[0] == '-' {
            v = v[1:]
        }
        for _, c := range v {
            if strings.IndexRune("0123456789.", c) == -1 {
                log.Fatal(error)
            }
        }
        s := strings.Split(v, ".")
        ls := len(s)
        if ls == 1 {
            return 0
        } else if ls == 2 {
            return len(s[1])
        } else {
            log.Fatal("Too many decimal points")
        }
    default:
        log.Fatal(error)
    }
    return 0
}

func main() {
    var a = []interface{}{12, 12.345, 12.345555555555, "12.3450", "12.34555555555555555555", 12.345e53}
    for _, n := range a {
        d := getNumDecimals(n)
        switch v := n.(type) {
        case string:
            fmt.Printf("%q has %d decimals\n", v, d)
        case float32, float64:
            fmt.Printf("%g has %d decimals\n", v, d)
        default:
            fmt.Printf("%d has %d decimals\n", v, d)
        }
    }
}
Task: Enumerations

C: enum fruits { apple, banana, cherry };

enum fruits { apple = 0, banana = 1, cherry = 2 };




Go: const (
	apple = iota
	banana
	cherry
)