Original post

I am a developer of c/c++. I started studying golang from today.
Go lang is similar to c/c++ and is simple so I think I will get used to it soon.
After studying for about an hour, there is a difference in syntax than I thought, but I don’t know how to solve it, so the process is slow.
For this reason, I would like to find a grammar that matches exactly c++.
Are there any helpful websites? I meet situations where even the following very simple array, variables, initializations, and repetitive statements are difficult, and they work differently than the intended situation.

like this…
c++

std::vector<std::string> test; // empty container ok
std::vector<std::string> test(2,"test"); // ok
std::vector<std::string> test = {"123","123123"} // ok
==> assignable by constructor and copy assignment through equal operator, so it's possible

go lang

var test []string //ok 
var test []string = {"123","345","456"} // not work
test := []string{"123","345","456"} // ok

==> i don't know why its not working or working

Take the tour and if you want, read the Go Programming Language Specification.

thank you ~

What example code will make my golang skill grow faster?

I always recommend to get a copy of the book “The Go Programming Language”.

And as with any programming language the advise is to start with small projects to solve and try to be part of the Go community.

The var keyword in Go only declares a identifier of a type . you cannot assing any value at that time.
While the short declaration := operator in Go declares an identifier as well as assigns a value to it.

Apart from language specification, see the Effective Go also.

GoDoc.org

See and learn from-
GoByExample

So according to what you say, var foo int = 1 ist invalid?

corrigendum : * slices

  • slices
    i missed that

As slices are compounds, you just need to construct them.properly.

I can’t test, as this is my mobile but the following should work, though I wouldn’t actually do it because that repetition annoys me.

var s []string = []string{"a", "b", "c"}

There are many ways to learn, the above are all good paths.

I used this course to jumpstart my entry into Go:

The best courses that I’ve found are Jon Calhoun’s courses. He has free courses on algorithms and some “gophercises”. He also has a paid web development course which is the best source that I have found on the subject.

The strength of Jon Calhoun is that, unlike many courses on Udemy, he actually explains how to put things together instead of just reading package documentation out loud.

He has a great discount during the pandemic. The best budget option for the web development course it to just get his book without screencasts, ~$40. The full course with screencasts is about $150, and very much worth it.

Or you can simply write:

var s = []string{"a", "b", "c"}

var keywoard is able to understand what type is after = sign.

first:var是一个关键字,用来声明变量使用的
然后看你给的这个例子
var test []string = {“123”,“345”,“456”}
上面这个是语法错误的,为什么呢?
原因,你应该这样去写
var test []string =[]string {“123”,“345”,“456”}
因为你做了一个赋值的操作
再看下面的例子
test := []string{“123”,“345”,“456”}
这个是声明了局部变量,然后,通过自动推导的方式实现的。

可以关注我的github,pi-pi-miao

sorry icant read this

That’s Chinese. You need a translation

No, you need to write English.

Recently digital ocean exposed us an wonderful content. Enjoy it! :smiley:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.