从Java到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.

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
")
    fd.WriteString("
Hello Gopher!
")
}

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
")
    fd.WriteString("
Hello Gopher!
")
    routine(fd)
}
package main

import (
    "fmt"
    "os"
)

func main() {
    fd, err := os.Create("out.txt")
    if err != nil {
        fmt.Println(os.Stderr, err)
        return
    }
    // defer calls a function at the end of the current function.           
    defer fd.Close()

    fd.WriteString("Hello Gopher!
")
}

I hope this helps. If this is not clear, please specify which part needs explanation.

For example,

package main

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

func main() {
    f, err := os.Create("out.txt")
    if err != nil {
        fmt.Println(err)
        return
    }
    defer f.Close()
    wtr := bufio.NewWriter(f)
    abc := 1
    _, err = wtr.WriteString("Hello Go " + strconv.Itoa(abc) + " ok
")
    if err != nil {
        fmt.Println(err)
        return
    }
    err = wtr.Flush()
    if err != nil {
        fmt.Println(err)
        return
    }
}

file out.txt:
Hello Go 1 ok

String literals

A string literal represents a string constant obtained from concatenating a sequence of characters. There are two forms: raw string literals and interpreted string literals.

Raw string literals are character sequences between back quotes ``. Within the quotes, any character is legal except back quote. The value of a raw string literal is the string composed of the uninterpreted characters between the quotes; in particular, backslashes have no special meaning and the string may span multiple lines.

Interpreted string literals are character sequences between double quotes "". The text between the quotes, which may not span multiple lines, forms the value of the literal, with backslash escapes interpreted as they are in character literals (except that \' is illegal and \" is legal). The three-digit octal ( nn) and two-digit hexadecimal (\xnn) escapes represent individual bytes of the resulting string; all other escapes represent the (possibly multi-byte) UTF-8 encoding of individual characters. Thus inside a string literal \377 and \xFF represent a single byte of value 0xFF=255, while ÿ, \u00FF, \U000000FF and \xc3\xbf represent the two bytes 0xc3 0xbf of the UTF-8 encoding of character U+00FF.