Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code Transformation from Java to Go

Tags:

go

I don't know syntax of Go. Can anybody help me to convert following java code in google's go language .

import java.io.*;

class FileWrite 
{
  public static void main(String args[])
  {
    try {
      // Create file 
      FileWriter fstream = new FileWriter("out.txt");
      BufferedWriter out = new BufferedWriter(fstream);
      out.write("Hello Java");
      // Close the output stream
      out.close();
    } catch (Exception e){ //Catch exception if any
      System.err.println("Error: " + e.getMessage());
    }
  }
}

This java code creates a file named out.txt and write a string (Hello Java) on the file.

like image 412
alessandro Avatar asked Sep 10 '26 00:09

alessandro


1 Answers

But, how this problem can solved ? play.golang.org/p/169zmQvK7m – alessandro

For example,

package main

import (
    "fmt"
    "os"
    "strconv"
)

func routine(fd *os.File) {
    abc := 1
    fd.WriteString("Hello Go " + strconv.Itoa(abc) + " ok\n")
    fd.WriteString("\nHello Gopher!\n")
}

func main() {
    fd, err := os.Create("out.txt")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer fd.Close()
    abc := 1
    fd.WriteString("Hello Go " + strconv.Itoa(abc) + " ok\n")
    fd.WriteString("\nHello Gopher!\n")
    routine(fd)
}
like image 87
peterSO Avatar answered Sep 13 '26 00:09

peterSO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!