Go笔记 2.1 第1个程序 helloworld

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/iotisan/article/details/79667056

我正在学习酷酷的 Golang,可点此查看帖子Golang学习笔记汇总

1 程序介绍

在 C:\Go\test 目录下,有一堆官方提供的示例。找到大部分教程都推荐的第1个程序,helloworld.go。

package main

func main() {
    print("hello, world\n")
}

发现没有,每个语句不需要像 C 家族中的其它语言一样以分号 ; 结尾,因为这些工作都将由 Go 编译器自动完成。

2 程序执行

进入目录 C:\Go\test>

执行命令 go run helloworld.go

C:\Go\test>go run helloworld.go
hello, world

3 总结

在接下来的几个章节里,将会对这个文件拆分,了解下Go的基础程序结构。

End


展开阅读全文

第9章第1程序的几个问题

09-12

[code=C/C++]rn#include rnrnstructrnrn int iStyle ;rn TCHAR * szText ;rnrnbutton[] =rnrn BS_PUSHBUTTON, TEXT ("PUSHBUTTON"),rn BS_DEFPUSHBUTTON, TEXT ("DEFPUSHBUTTON"),rn BS_CHECKBOX, TEXT ("CHECKBOX"), rn BS_AUTOCHECKBOX, TEXT ("AUTOCHECKBOX"),rn BS_RADIOBUTTON, TEXT ("RADIOBUTTON"),rn BS_3STATE, TEXT ("3STATE"),rn BS_AUTO3STATE, TEXT ("AUTO3STATE"),rn BS_GROUPBOX, TEXT ("GROUPBOX"),rn BS_AUTORADIOBUTTON, TEXT ("AUTORADIO"),rn BS_OWNERDRAW, TEXT ("OWNERDRAW")rn ;rnrn#define NUM (sizeof button / sizeof button[0])rnrnLRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;rnrnint WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,rn PSTR szCmdLine, int iCmdShow)rnrn static TCHAR szAppName[] = TEXT ("BtnLook") ;rn HWND hwnd ;rn MSG msg ;rn WNDCLASS wndclass ;rn rn wndclass.style = CS_HREDRAW | CS_VREDRAW ;rn wndclass.lpfnWndProc = WndProc ;rn wndclass.cbClsExtra = 0 ;rn wndclass.cbWndExtra = 0 ;rn wndclass.hInstance = hInstance ;rn wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;rn wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;rn wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;rn wndclass.lpszMenuName = NULL ;rn wndclass.lpszClassName = szAppName ;rn rn if (!RegisterClass (&wndclass))rn rn MessageBox (NULL, TEXT ("This program requires Windows NT!"),rn szAppName, MB_ICONERROR) ;rn return 0 ;rn rn rn hwnd = CreateWindow (szAppName, TEXT ("Button Look"),rn WS_OVERLAPPEDWINDOW,rn CW_USEDEFAULT, CW_USEDEFAULT,rn CW_USEDEFAULT, CW_USEDEFAULT,rn NULL, NULL, hInstance, NULL) ;rn rn ShowWindow (hwnd, iCmdShow) ;rn UpdateWindow (hwnd) ;rn rn while (GetMessage (&msg, NULL, 0, 0))rn rn TranslateMessage (&msg) ;rn DispatchMessage (&msg) ;rn rn return msg.wParam ;rnrnrnLRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)rnrn static HWND hwndButton[NUM] ;rn static RECT rect ;rn static TCHAR szTop[] = TEXT ("message wParam lParam"),rn szUnd[] = TEXT ("_______ ______ ______"),rn szFormat[] = TEXT ("%-16s%04X-%04X %04X-%04X"),rn szBuffer[50] ;rn static int cxChar, cyChar ;rn HDC hdc ;rn PAINTSTRUCT ps ;rn int i ;rn rn switch (message)rn rn case WM_CREATE :rn cxChar = LOWORD (GetDialogBaseUnits ()) ;rn cyChar = HIWORD (GetDialogBaseUnits ()) ;rn rn for (i = 0 ; i < NUM ; i++)rn hwndButton[i] = CreateWindow ( TEXT("button"), rn button[i].szText,rn WS_CHILD | WS_VISIBLE | button[i].iStyle,rn cxChar, cyChar * (1 + 2 * i),rn 20 * cxChar, 7 * cyChar / 4,rn hwnd, (HMENU) i,rn ((LPCREATESTRUCT) lParam)->hInstance, NULL) ;rn return 0 ;rnrn case WM_SIZE :rn rect.left = 24 * cxChar ;rn rect.top = 2 * cyChar ;rn rect.right = LOWORD (lParam) ;rn rect.bottom = HIWORD (lParam) ;rn return 0 ;rn rn case WM_PAINT :rn InvalidateRect (hwnd, &rect, TRUE) ; //问题2:这里使用InvalidateRect和下面ValidateRect产生了一个什么样的逻辑关系?能否把RECT从无效到有效并绘制出消息的这一过程为我解说一遍rn rn hdc = BeginPaint (hwnd, &ps) ;rn SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;rn SetBkMode (hdc, TRANSPARENT) ;rn rn TextOut (hdc, 24 * cxChar, cyChar, szTop, lstrlen (szTop)) ;rn TextOut (hdc, 24 * cxChar, cyChar, szUnd, lstrlen (szUnd)) ;rn rn EndPaint (hwnd, &ps) ;rn return 0 ;rn rn case WM_DRAWITEM :rn case WM_COMMAND :rn ScrollWindow (hwnd, 0, -cyChar, &rect, &rect) ;rn rn hdc = GetDC (hwnd) ;rn SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;rn rn TextOut (hdc, 24 * cxChar, cyChar * (rect.bottom / cyChar - 1),rn szBuffer,rn wsprintf (szBuffer, szFormat,rn message == WM_DRAWITEM ? TEXT ("WM_DRAWITEM") : rn TEXT ("WM_COMMAND"),rn HIWORD (wParam), LOWORD (wParam),rn HIWORD (lParam), LOWORD (lParam))) ;rn rn ReleaseDC (hwnd, hdc) ;rn ValidateRect (hwnd, &rect) ; //问题2中的ValidateRectrn break ; //问题1:这里为何用break而不用return 0,他再把WM_DRAWITEM和WM_COMMAND发给defwndProc有什么效果rnrn rn case WM_DESTROY :rn PostQuitMessage (0) ;rn return 0 ;rn rn return DefWindowProc (hwnd, message, wParam, lParam) ;rnrnrn//问题3:为什么这个程序一运行就产生了WM_DRAWITEMrn[/code]rnrn3个问题见代码注释处rn谢谢 论坛

没有更多推荐了,返回首页