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条)

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable