What Java users thought of using the Go language for a day

Introduction

The title says one day, but I started it about the day before yesterday, and did it take about eight hours in total? I thought, so I will summarize my impressions.

String concatenation

Often, "+" is used to combine strings in a loop. .. .. I see a typical article, but it's not the case of a combination of strings + numbers.

In Java

int bread = 13;
System.out.println("Do you remember how many breads you have ever eaten?");
System.out.println(bread + "I'm Japanese food");

I write it in a similar way.

With Golang (I'm sorry I omit import)

bread := 13
fmt.Println("Do you remember how many breads you have ever eaten?")
fmt.Printf("%d sheets I'm Japanese food\n", bread)

Will be.

After that, you can convert the type.

bread := 13
breadStr := strconv.Itoa(bread)

No ternary operator

I wrote a syntax error when I wanted to return a value with a ternary operator. There isn't. .. ..

In Java

boolean isOk = true;
String text = isOk ? "OK" : "NG";

The guy.

With Golang

isOk := true
text := "NG"
if isOk {
    text = "OK"
}

etc.

Loop processing is for statement only

With Java8 and StreamAPI, I write more and more using method chains, but I can't do that. There is something similar to the extended for statement, but you need to be a little careful.

In Java

String[] array = {"a", "b", "c"};

//Stream API
Arrays.stream(array).peek(System.out::println);

//Extended for statement
for (String text : array) {
    System.out.println(text);
}

The guy.

With Golang

NG.go


array := []string{"a", "b", "c"}

for text := range array {
    fmt.Println(text)
}

That's no good

OK.go


array := []string{"a", "b", "c"}

for _, text := range array {
    fmt.Println(text)
}

is not it. Since the range that can be used in the for statement is Multiple Return Values, the first return value is the index and the second return value is the value stored in the array. Please note that omitting the second case will not cause an error.

Other things

I haven't touched much about the asynchronous surroundings such as goroutine and channel, and the relationship between interface {} and type, so I'll write it after touching it a little more.

in conclusion

Go language is interesting! (Is it fun to learn a new language?)

I have some troubles when writing various things, but I thought it was relatively easy to get along with.

Recommended Posts

What Java users thought of using the Go language for a day
Avoiding the pitfalls of using a Mac (for Linux users?)
Java programmer tried to touch Go language (for the time being)
Impressions of using Flask for a month
The story of introducing a multi-factor authentication function using a one-time password into a Java application
Cut a part of the string using a Python slice
Ruby expert learned the basic grammar of Go language
Check for the existence of BigQuery tables in Java
What is a recommend engine? Summary of the types
What I thought after working on the "No comment at all" project for a year
Does TensorFlow change the image of deep learning? What I thought after touching a little
The story of creating a VIP channel for in-house chatwork
Reuse the behavior of the @property method by using a descriptor [16/100]
A note for embedding the scripting language in a bash script
Try a similar search for Image Search using the Python SDK [Search]
Note 2 for embedding the scripting language in a bash script
The story of creating a database using the Google Analytics API
What is the interface for ...
A memorandum of using eigen3
A memorandum of understanding for the Python package management tool ez_setup
GraalVM's memory usage can be very low. What a 1/16 of Java!
Created gomi, a trash can tool for rm in Go language
100 Language Processing Knock-96 (using Gensim): Extraction of vector for country name
Evaluate the performance of a simple regression model using LeaveOneOut cross-validation
Read the config file in Go language! Introducing a simple sample
Try using Elasticsearch as the foundation of a question answering system
Finding the optimum value of a function using a genetic algorithm (Part 1)
Create a function to get the contents of the database in Go
Can the F1C100s dream of embedded development for mediocre Linux users?
[Kaggle] I made a collection of questions using the Titanic tutorial
The story of creating a store search BOT (AI LINE BOT) for Go To EAT in Chiba Prefecture (1)
A story about trying to improve the testing process of a system written in C language for 20 years