代码从Java转换为Go

huangapple go评论69阅读模式
英文:

Code Transformation from Java to Go

问题

package main

import (
"fmt"
"os"
)

func main() {
// Create file
file, err := os.Create("out.txt")
if err != nil {
fmt.Println("Error:", err)
return
}
defer file.Close()

// Write string to file
_, err = file.WriteString("Hello Java")
if err != nil {
	fmt.Println("Error:", err)
	return
}

}

英文:

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.

答案1

得分: 3

package main

import (
"fmt"
"os"
)

func main() {
fd, err := os.Create("out.txt")
if err != nil {
fmt.Println(os.Stderr, err)
return
}
// defer在当前函数结束时调用一个函数。
defer fd.Close()

fd.WriteString("Hello Gopher!\n")

}

英文:
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!\n")
}

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

答案2

得分: 3

例如,

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\n")
	if err != nil {
		fmt.Println(err)
		return
	}
	err = wtr.Flush()
	if err != nil {
		fmt.Println(err)
		return
	}
}

文件 out.txt:
Hello Go 1 ok\n

字符串字面量

字符串字面量表示通过连接一系列字符获得的字符串常量。有两种形式:原始字符串字面量和解释字符串字面量。

原始字符串字面量是位于反引号``之间的字符序列。在引号内,除了反引号之外,任何字符都是合法的。原始字符串字面量的值是由引号之间的未解释字符组成的字符串;特别地,反斜杠没有特殊含义,字符串可以跨越多行。

解释字符串字面量是位于双引号""之间的字符序列。引号之间的文本不能跨越多行,它构成了字面量的值,其中反斜杠转义被解释为字符字面量中的转义(除了'是非法的,"是合法的)。三位八进制(\nnn)和两位十六进制(\xnn)转义表示结果字符串的单个字节;所有其他转义表示结果字符串的(可能是多字节的)UTF-8编码的单个字符。因此,在字符串字面量内部,\377和\xFF表示值为0xFF=255的单个字节,而ÿ,\u00FF,\U000000FF和\xc3\xbf表示UTF-8编码的字符U+00FF的两个字节0xc3 0xbf。

英文:

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\n")
	if err != nil {
		fmt.Println(err)
		return
	}
	err = wtr.Flush()
	if err != nil {
		fmt.Println(err)
		return
	}
}

file out.txt:
Hello Go 1 ok\n

> 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 (\nnn) 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.

答案3

得分: 3

例如,

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)
}
英文:

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

huangapple
  • 本文由 发表于 2011年12月1日 02:04:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/8330685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定