Golang (1)

Source: Internet
Author: User
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

  1. package main

  2. func Main ( {
  3.     print ( "Hello world\n" )
  4. }

Click ( here ) to collapse or open

  1. #include<stdio. H>
  2. void main()
  3. {
  4. printf ("Hello world\n");
  5. }


Like C, go also needs a main function;
Different:
    1. function more than one keyword func;
    2. The first {must be immediately behind the parentheses, not a new line!
    3. Function print is a few built-in functions of go. Prinf is the library function in the stdio;
    4. The main function must be the package main;
    5. The end of the semicolon can not write Oh. Of course you have a few sentences, you can not save.
    6. 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

  1. Package Main

  2. Func main(){
  3. var i int
  4. var j int8
  5. I = 1
  6. J=i+1
  7. Print (i,"\ t", J)
  8. }


Click ( here ) to collapse or open

  1. #include<stdio. H>
  2. void main()
  3. {
  4. int i;
  5. Char J;
  6. I=1;
  7. J=i+1
  8. printf("%d\t%d\n",i,j);
  9. }


Except for line fifth, it's like everything else is different.
Different:
    1. 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;
    2. 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.
    3. 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.
    4. 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

  1. Package Main
  2. Func main(){
  3. A := 0
  4. for i := 1; I< 10; I++{
  5. for i := 1; I < 10; I++{
  6. A += I
  7. Print (a,"\ n")
  8. }
  9. }
  10. }
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;
    1. 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!
    2. 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!
    3. 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

  1. Package Main

  2. Func main(){
  3.     var a = [ ] int { 10 25 32 11 6 36 18 22 5 7}
  4.     / /      var a[ 10] int = [ 10 int { 10 25 32 11 6 36 18< Span style= "COLOR: #0000CC", 22 5, 7 }
  5.     / /      var a[ 10] int = [ ] int { 10 25 32 11 6 36 18 22 5 7}
  6. for i := 0; i< len(a); i+ +{
  7. forWhile:=I;WHile< Len(A);WHile++{
  8. if a[i]> a[while]{
  9. A[I],A[WHile]=A[WHile],A[I]
  10. }
  11. }
  12. }    
  13. i:=0
  14. for I <len(a) {
  15. Print(a[i],"\ T")
  16. i++
  17. }
  18. }

    1. 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:=;
    2. The array can be of length, a bit like java.
    3. 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.
    4. 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.
    5. 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.
    6. 16,17 Why not merge? Try it yourself. Syntax error OH. In the go document, the compiler will automatically add semicolons after the + +.






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.