Input
Output

C to golang converter online

Do you want to convert your C code to relevant equivalent Go code?, Then this can prove to be the best place for you. It's a free tool that converts custom functions to similar Go code.

C to go examples

Here mentioned few examples which is work on structure, array, ternary operator and loops.

Struct example

C code:

/*
    C program for
    Insert linked list node at beginning of linked list
*/
#include <stdio.h>
#include <stdlib.h>

// Linked List LinkNode
struct LinkNode
{
    int data;
    struct LinkNode *next;
};
// Singly linked list 
struct SingleLL
{
    struct LinkNode *head;
};
// Returns the new linked list
struct SingleLL *newLinkedList()
{
    // Create memory of head and tail Nodes
    struct SingleLL *sll = (struct SingleLL *)
    malloc(sizeof(struct SingleLL));
    if (sll == NULL)
    {
        printf("Memory overflow\n");
    }
    else
    {
        sll->head = NULL;
    }
    return sll;
}
// Handles the request of adding new node at beginning of linked list
void addNode(struct SingleLL *sll, int data)
{
    // Create dynamic node
    struct LinkNode *node = (struct LinkNode *)
    malloc(sizeof(struct LinkNode));
    if (node == NULL)
    {
        printf("Memory overflow to Create LinkNode\n");
        return;
    }
    else
    {
        // Set initial node value
        node->data = data;
        node->next = sll->head;
    }
    sll->head = node;
}
// Display linked list element
void display(struct LinkNode *node)
{
    if (node == NULL)
    {
        return;
    }
    struct LinkNode *temp = node;
    // iterating linked list elements
    while (temp != NULL)
    {
        printf(" %d →", temp->data);
        // Visit to next node
        temp = temp->next;
    }
    printf(" NULL\n");
}
int main()
{
    struct SingleLL *sll = newLinkedList();
    // Linked list
    // 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
    addNode(sll, 8);
    addNode(sll, 7);
    addNode(sll, 6);
    addNode(sll, 5);
    addNode(sll, 4);
    addNode(sll, 3);
    addNode(sll, 2);
    addNode(sll, 1);
    printf(" Linked List \n");
    display(sll->head);
    return 0;
}

Go code:

package main
import "fmt"
/*
    C program for
    Insert linked list node at beginning of linked list
*/
// Linked List LinkNode
type LinkNode struct {
    data int
    next* LinkNode
}
// Singly linked list
type SingleLL struct {
    head* LinkNode
}
// Returns the new linked list
func newLinkedList()* SingleLL {
    // Create memory of head and tail Nodes
    var sll* SingleLL = &SingleLL{}
    if sll == nil {
    {
        fmt.Printf("Memory overflow\n")
    }
    } else {
        sll.head = nil
    }
    return sll
}
// Handles the request of adding new node at beginning of linked list
func addNode(sll* SingleLL, data int) {
    // Create dynamic node
    var node* LinkNode = &LinkNode{}
    if node == nil {
    {
        fmt.Printf("Memory overflow to Create LinkNode\n")
        return
    }
    } else {
        // Set initial node value
        node.data = data
        node.next = sll.head
    }
    sll.head = node
}
// Display linked list element
func display(node* LinkNode) {
    if node == nil {
    {
        return
    }
    }
    var temp* LinkNode = node
    // iterating linked list elements
    for temp != nil    {
        fmt.Printf(" %d →",temp.data)
        // Visit to next node
        temp = temp.next
    }
    fmt.Printf(" NULL\n")
}
func main() {
    var sll* SingleLL = newLinkedList()
    // Linked list
    // 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → NULL
    addNode(sll, 8)
    addNode(sll, 7)
    addNode(sll, 6)
    addNode(sll, 5)
    addNode(sll, 4)
    addNode(sll, 3)
    addNode(sll, 2)
    addNode(sll, 1)
    fmt.Printf(" Linked List \n")
    display(sll.head)
}

Array example

C code:

#include<stdio.h>
void main()
{
    // Tested Code
    int a [] = {1,2,3};
    int b [][3] = {{1,2,3},{4,5,6}};
    printf("%d\n",a[1]);
    printf("%d\n",b[1][0]);
}

Golang code: Array declaration

package main
import "fmt"
func main() {
    // Tested Code
    a:=[] int {1, 2, 3}
    b:=[][3] int {{1, 2, 3}, {4, 5, 6}}
    fmt.Printf("%d\n",a[1])
    fmt.Printf("%d\n",b[1][0])
}

Ternary operator example

C code:

#include <stdio.h>

void main()
{
	int a = 10;
	int b = 20;
	int c = (a > b) ? a + 2 : b + 5;
	int d = (c > 2) ? ((c > b) ? c - 3 : c + 3) : 100;
	// 25
	printf("%d\n", c);
	// 22
	printf("%d\n", d);
}

Golang code: Ternary operator in golang.

package main
import "fmt"
func main() {
    var a int = 10
    var b int = 20
    var c int = (map[bool] int {true: a + 2,false:b + 5})[(a > b)]
    var d int = (map[bool] int {true: (map[bool] int {true: c - 3,false:c + 3})[(c > b)],false:100})[(c > 2)]
    // 25
    fmt.Printf("%d\n",c)
    // 22
    fmt.Printf("%d\n",d)
}

For loop example

C code:

#include <stdio.h>
int main()
{
	for (int i = 0; i < 5; i++)
	{
		for (int j = 0; j < 2; j++)
		{
			printf("%d", i + j + 1);
		}
		printf("\n");
	}
}

Golang code:

package main
import "fmt"
func main()  {
    var i int = 0
    for  i < 5 {
        var j int = 0
        for  j < 2 {
            fmt.Printf("%d",(i + j) + 1)
            j++
        }
        fmt.Printf("\n")
        i++
    }
}

While loop example

C code:

#include<stdio.h>

int main()
{
	int i = 0;
	while (i < 5)
	{
		int j = 0;
		while (j < 2)
		{
			printf("%d", i + j + 1);
			j++;
		}
		printf("\n");
		i++;
	}
}

Golang code:

package main
import "fmt"
func main()  {
    var i int = 0
    for  i < 5 {
        var j int = 0
        for  j < 2 {
            fmt.Printf("%d",(i + j) + 1)
            j++
        }
        fmt.Printf("\n")
        i++
    }
}

Do-while loop example

#include<stdio.h>
int main()
{
    int i = 0;
	do {
	    printf("%d",i);
	    i--;
	}while(i > 0);
}
package main
import "fmt"
func main()  {
    var i int = 0
    for true {
        fmt.Printf("%d",i)
        i--
        if !(i > 0) { break }
    }
}

Note : This tool are designed to convert c code to similar golang code statically. It is in its infancy and does not provide 100% accuracy guarantee.


Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment