douzhanshen0657 2011-11-30 18:04
浏览 338
已采纳

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

  • 写回答

3条回答 默认 最新

  • doujiacai4986 2011-11-30 20:20
    关注

    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)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?