This is a creation in Article, where the information may have evolved or changed. Always want to have a language like Java to automatically manage memory, and like C has pointers; yesterday in the blog home to see Go introduction, suddenly found himself already very outdated, dream language Golang has come out so long, oneself just know. Because the C is more familiar, the object-oriented Java is relatively familiar. So the preliminary involved Golang, always want to C in these expressions in Golang how to write;
A HelloWorld
Well, first of all, a hello,world, the left is go, the right is C; hehe, we'll know when we see it.
Click () to collapse or open
- package main
-
- func Main ( {
- print ( "Hello world\n" )
- }
|
Click ( here ) to collapse or open
- #include<stdio. H>
- void main()
- {
- printf ("Hello world\n");
- }
|
Like C, go also needs a main function;
Different:
- function more than one keyword func;
- The first {must be immediately behind the parentheses, not a new line!
- Function print is a few built-in functions of go. Prinf is the library function in the stdio;
- The main function must be the package main;
- The end of the semicolon can not write Oh. Of course you have a few sentences, you can not save.
- Other not obvious differences for the time being don't say it;
Two variable types
Now let's look at the variable type. Go is almost like C. There are definite types of variables, compared to JavaScript, VAR is based on the operation to determine the type, the sense of efficiency is always problematic, and sometimes even you do not know what type.
Go defines the following types of integers: int8, Int16, Int32, Int64 (signed. Very good ah, directly indicate the number of bits, do not know if someone originally remember Char,short,int,long length also took time, now do not have, unsigned, directly add a u,uint8, UInt16, UInt32, UInt64 float is only float32,float64; Of course they also have int, this int is 32 bits, or 64 bits, depending on the number of machines, and byte=int8; and a string type, think of it like a string in Java. , it is impossible to change one of these characters unless a new string is generated;
All right, here's an addition operation.
Click ( here ) to collapse or open
- Package Main
- Func main(){
- var i int
- var j int8
- I = 1
- J=i+1
- Print (i,"\ t", J)
- }
|
Click ( here ) to collapse or open
- #include<stdio. H>
- void main()
- {
- int i;
- Char J;
- I=1;
- J=i+1
- printf("%d\t%d\n",i,j);
- }
|
Except for line fifth, it's like everything else is different.
Different:
- The declaration of a variable is the first keyword VAR, then the variable i, then the type int, and the C-turn, and a keyword var; why should it be reversed? In the official documentation, we may have missed some of the omissions in C. such as: int *a,b; Two different types A is int *; b is int; it's easy to misinterpret it as a pointer. There's no problem in go. var a, b * int; Hehe, all is the integral type pointer;
- Print is not the same, different content directly with the good separation, as if there is no good C, especially when a large number of documents inserted in the number, to divide a lot of broken. Is there a format function like C? Let's learn it slowly.
- Print parameters are not strictly required, both recognize I is integral type, have the knowledge "\ n" is a string; This is much stronger than C.
- If you compile, you will find that the 7th line on the left has a syntax error. Because the type of I and J is different, and C is not so strict. Report a warning, while go in need to be explicitly written j=int8 (i+1); Seems a bit annoying oh! Attention is int8 (i+1)!
Three for
OK, let's have a for-loop.
Click ( here ) to collapse or open
- Package Main
- Func main(){
- A := 0
- for i := 1; I< 10; I++{
- for i := 1; I < 10; I++{
- A += I
- Print (a,"\ n")
- }
- }
- }
The code is a little strange, and a little bit unaware of what it means, yes, there is no purpose, is to introduce the declaration of variables, and scope, and for the wording;
- A:=0 is a shorthand for variable declarations, and a is what type has the following assignment decision; it can only be written once. Next time you want to assign a value to write a=**** directly; What kind of a is this? int8, Int16 ...? Write a small program to try it!
- Suspected? How did i:= use it two times? You will find that the two I are not the same as the following printed results. The respective scopes are their for loops, and the second I covers the first I Oh!
- Take a look at for, as if there's something missing. That's right. The brackets are gone. And there must be no Oh, and the opening curly braces cannot be another line.
Hey, isn't that a little messy? But for those of us who have the basic old programmer, that has the mind to go to one one to see the explanation. Read a good paragraph, a program also can not write. See the back again to forget the front. Well, go ahead, have so many to take a bubble.
Four bubbles
Click ( here ) to collapse or open
- Package Main
- Func main(){
- var a = [ ] int { 10 25 32 11 6 36 18 22 5 7}
- / / var a[ 10] int = [ 10 int { 10 25 32 11 6 36 18< Span style= "COLOR: #0000CC", 22 5, 7 }
- / / var a[ 10] int = [ ] int { 10 25 32 11 6 36 18 22 5 7}
- for i := 0; i< len(a); i+ +{
- forWhile:=I;WHile< Len(A);WHile++{
- if a[i]> a[while]{
- A[I],A[WHile]=A[WHile],A[I]
- }
- }
- }
- i:=0
- for I <len(a) {
- Print(a[i],"\ T")
- i++
- }
- }
- There are three types of initialization of the array, the first one is the simplest, there are several compilers themselves, reducing the burden on many programmers. As for the latter two, must be 10 oh. Otherwise, if your request must be 10, or write a number, let the compiler help you check, to prevent the wrong! Of course, can also be written a:=;
- The array can be of length, a bit like java.
- Always have a dream, want to define a variable called while, how can not be achieved, today go to help me realize. There is no while keyword in go. Only for. Use the fewest keywords. Go is really a programmer's guess.
- Look at line tenth, please. Two variable exchange, a sentence is completed, in C how to also three lines of code; There is also an intermediate variable; This is not a compilation statement.
- Although there is no while, go also did not forget our habits, the two semicolon on both sides of the assignment is removed, for the programming while format. For while (true) is simple. A for is done.
- 16,17 Why not merge? Try it yourself. Syntax error OH. In the go document, the compiler will automatically add semicolons after the + +.